Search code examples
pythonodooodoo-14validationerror

Validationerror when field is empty


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


Solution

  • 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'