Skip to Content
Menu
This question has been flagged
3 Replies
1803 Views

hi,i have form for the parent of a student on which i have a field named "CNIC_NO",,,what i want is that when i enter a cnic number less than or greater than 13it should give error,,,what could be the code??i have tried but its not working correctly...

here is my code


@api.onchange('cnic_no')
def add_validation(self):
if self.cnic_no:
if len(self.cnic_no) < 15:
raise ValidationError('Please Enter correct number of cnin digits \n Thank You !')
Avatar
Discard
Author

i have tried 13 in the code too,

Best Answer

Validation does not work on onchange. Use api.constrains for this.

@api.constrains('cnic_no')
def add_validation(self):
if self.cnic_no:
if len(self.cnic_no) < 15:
raise ValidationError('Please Enter correct number of cnin digits \n Thank You !')


Avatar
Discard
Best Answer

Hi,

From onchange you can return warning like this,

@api.onchange('product_uom_qty')
def _onchange_product_uom_qty(self):
if self.state == 'sale' and self.product_id.type in ['product', 'consu'] and self.product_uom_qty < self._origin.product_uom_qty:
warning_mess = {
'title': _('Ordered quantity decreased!'),
'message' : _('You are decreasing the ordered quantity! Do not forget to manually update the delivery order if needed.'),
}
return {'warning': warning_mess}

return {}

But this won't stop the execution of the code. If you want to show validation in the onchange itself you can try the above code and reset the wrongly entered value to null or False, and ask the user to enter it again correctly or by making the required field.

Else you can try the solution that Mr Sudhir has mentioned with the ValidationError using constrains, see this: How To Add Constrains For A Field in Odoo12

Thanks

Avatar
Discard