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.

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

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi Helder,

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

      Delete
    2. This comment has been removed by the author.

      Delete
  2. Hello,
    This 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


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

      Delete
    2. Hello

      I 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


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

      Delete

ImportError: cannot import name 'utils' from 'PyPDF2'

Odoo 15: Traceback (most recent call last):   File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner     self...