This article will help us to validate Email field on Partner form.
First of all, we need to understand argument of _constraints.
_constraint has list of arguments in which
1. Method is name,
2. Warning/Error message and
3. List of field name.
Method will be fire based on, change of field value given as third argument in _constraint.
Now we need to set constraint for Email field.
Here is code for constraint.
_constraints = [
(_validate_email, 'Please enter a valid email address.', ['email']),
]
Here is code for method.
@api.multi
def _validate_email(self):
for partner in self:
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", partner.email) == None:
return False
return True
NOTE:
Warning/Error message will be populate, only when method will return False.
I hope you like this article. Share your views to improve content. Happy Learning !!!
First of all, we need to understand argument of _constraints.
_constraint has list of arguments in which
1. Method is name,
2. Warning/Error message and
3. List of field name.
Method will be fire based on, change of field value given as third argument in _constraint.
Now we need to set constraint for Email field.
Here is code for constraint.
_constraints = [
(_validate_email, 'Please enter a valid email address.', ['email']),
]
Here is code for method.
@api.multi
def _validate_email(self):
for partner in self:
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", partner.email) == None:
return False
return True
NOTE:
Warning/Error message will be populate, only when method will return False.
I hope you like this article. Share your views to improve content. Happy Learning !!!
This comment has been removed by the author.
ReplyDeleteHi Helder,
DeleteCan you provide more information? For which object are you looking for?
This comment has been removed by the author.
DeleteHello,
ReplyDeleteThis is my py code for validation of email field in odoo 10
@api.onchange('email')
def onchange_email(self):
if not self.email:
return {}
if not self.email("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
raise UserError(_('Invalid Email', 'Please enter a valid email address'))
return {}
this my xml code for the email field.
I'm getting an error can u pls help me with this .....
Thank you
Where is your code and can you please post your error as well ?
DeleteHello
DeleteI have this code in customer module and this field needs to be validated...
@api.one
@api.onchange('emai')
def validateEmail(selfl):
if self.email:
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
return {}
else:
raise raise except_orm(_('Invalid Email'), _("Please enter a valid email address"))
This is my py code.... but when I upgrade the module with this code it returns an error saying...
ParseError: "Error while validating constraint
Can you tried with same code as written in Blog. You have tried to return dictionary {} and raise error. That's may cause. Generally, Constraint function return True or False. If we want to raise error then we should return False or allow to save record then return True. I hope this make sense to you. Cheers.
Delete