Sunday, 27 September 2015

Email validation in Odoo

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.

    def _validate_email(self):
        for partner in self:
            if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", == None:
                return False
        return True


         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 !!!


  1. This comment has been removed by the author.

    1. Hi Helder,

      Can you provide more information? For which object are you looking for?

    2. This comment has been removed by the author.

  2. Hello,
    This is my py code for validation of email field in odoo 10

    def onchange_email(self):
    if not
    return {}
    if not"^.+\\@(\\[?)[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

    1. Where is your code and can you please post your error as well ?

    2. Hello

      I have this code in customer module and this field needs to be validated...
      def validateEmail(selfl):

      if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
      return {}
      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

    3. 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.

  3. I really impressed with your interesting blog and found some information about Odoo Partner US.Subscribed your blog.Thanks for sharing.


ImportError: No module named googleapiclient

Recently, I have faced ImportError: No module named googleapiclient and I would like to share knowledge to fix that problem. Open termina...