ODOO
constrains
to explain this let us say that we have four-digit name as we explain how we can create it in this topic how to Add a four-digit name in odoo
now let us say that we have to create validation function that make the user can not save any name that contain any letter or mark than English letters , and to do this we must create constraint function that looping in the name of the employee and show validation error message if the name contain some non English letters or numbers , so the function can be as bellow
@api.constrains('first_name','second_name','third_name','fourth_name')
def _arabic_name_validity(self):
"""
verifies if name for the employee .
"""
arabic_letters_list = [
'a','s','d','f',
'k','j','h','g',
'l','z','x','c',
'm','n','b','v',
'q','w','e','r',
't','y','u','i',
'p','o',' ',
]
if self.first_name and self.second_name and self.third_name and self.fourth_name:
for index in range(0,len(self.first_name)):
if self.first_name[index] not in arabic_letters_list:
raise ValidationError(_('the first name of the employee name must contain english letters only'))
for index in range(0,len(self.second_name)):
if self.second_name[index] not in arabic_letters_list:
raise ValidationError(_('the first name of the employee name must contain english letters only'))
for index in range(0,len(self.third_name)):
if self.third_name[index] not in arabic_letters_list:
raise ValidationError(_('the first name of the employee name must contain english letters only'))
for index in range(0,len(self.fourth_name)):
if self.fourth_name[index] not in arabic_letters_list:
raise ValidationError(_('the first name of the employee name must contain english letters only'))
the validation message will look like the image bellow
No comments:
Post a Comment