Search code examples
pythonodoo

Validating mobile number - Odoo


In my xml file, i have the below field under a tree structure.

<field name="phone"/>

I have the below group under the form structure

<group>
<field name="phone" onchange="validate_phone(phone)" placeholder="Enter your mobile number"/>
</group>

In my python file:


    @api.depends('phone')
    @api.onchange('phone')
    def validate_phone(self):
        if self.phone:
            match = re.match('^[0-9]\d{10}$', self.phone)
            if match == None:
               raise ValidationError('Invalid')

There will be no error in code but still it does not work. Could any one help me to get through this?


Solution

  • First of all there is no need to use onchange attribute in xml keep it simple like

    <group>
        <field name="phone" placeholder="Enter your mobile number"/>
    </group>
    

    Second try this on just using onchange decorator

    @api.onchange('phone')
    def validate_phone(self):
        if self.phone:
            match = re.match('^[0-9]\d{10}$', self.phone)
            if not match:
               raise ValidationError('Invalid')