Validationerror when field is empty

242 Views Asked by At

I have created a Validation Error to show up when the submit button is clicked and the condition of the field is empty. But when I try to test it when the field is filled, the validation error still showing up. What's wrong with my code ?

def action_approved(self):
    for rec in self:
        expense_account = self.env['account.pettycash.voucher.wizard.line'].search([('expense_account','=',False)])
        if expense_account :
            raise ValidationError('Fill the expense account!')
        else :
            rec.state = 'approved'

I expect validation error to show up when field is empty and approve when filled

2

There are 2 best solutions below

0
Kenly On BEST ANSWER

To check if one expense account in voucher lines is not set and raise a validation error, loop over voucher_line field and check if expense_account is not set (if not line.expense_account then raise the validation error.

Example:

def action_approved(self):
    for rec in self:
        for line in rec.voucher_line:
            if not line.expense_account:
                raise ValidationError('Fill the expense account!')
        rec.state = 'approved'
1
NinjaBat On

Guess, you are checking the whole table. How is the class account.pettycash.voucher.wizard.line related to current self ? You need to include that as well in search condition.

expense_account = self.env['account.pettycash.voucher.wizard.line'].search([
('expense_account','=',False),
('relational_field', '=', self.id) #Need this condition as well.
])