Odoo Help

0

Show warning message when enter wrong mobile no. format in Odoo 10

By
Pawan Sharma
on 11/6/17, 2:21 AM 1,513 views

Hello all,

I have a integer field 'is_mobile'. Where user entering mobile. I want that when user enter wrong mobile no. format then it show warning message "invalid no. format" and also form not save without right format. For this i applied code, but it is not work as per requirement. 

Code is below:

is_mobile = fields.Integer("Mobile")


@api.multi

    @api.constrains('is_mobile')

    def _check_phone_number(self):

        for rec in self:

            if rec.is_mobile and len(rec.is_mobile) != "^[0-9]{10}$" :

                raise ValidationError(_("Wrong value enter"))

            else:

                return False

        return {}


Thanks in advance

1
Parth Patel (pat)
On 11/6/17, 8:23 AM

Hello Pawan,
Specific about your python condition :  

if rec.is_mobile and len(rec.is_mobile) != "^[0-9]{10}$" :
I Could not understand why you need to compare regex string with length, as it going to directly compare the string only not regex output.
So, rather you can directly do something like this :
if rec.is_mobile and len(str(rec.is_mobile)) != 10 :

Hii @Parth, Your code upto 10 digits work well, But after 10 digits it shows error "The following fields are invalid: " also warning message change into error when using character and other type words. I want that warning show else than 10 digits in any condition.

Thanks for help.

SunArc Technologies, Pawan Sharma
on 11/7/17, 12:09 AM

Hi Pawan, The Error warning 'The following fields are invalid: ' is due to you are using is_mobile as an Integer field and it can store max 10 digits, more than that it will show error. To, avoid this you can take Char field insted.

Just sake of information :

If you are about to develope something in odoo you can even use the default phone validation module provided by odoo. Where you have to just define phone widget in xml view.

<field name="phone" widget="phone"/>

And make sure module 'phone_validation' will install. This module will validate the phone number according to the country selected.

Parth Patel (pat)
on 11/7/17, 12:34 AM

Thanks parth, Now error message problem solved, but there is still one problem is that it take 10 values they might be integer, character, comma and etc. But i want only 10 integer values else validation. So, how i solve this problem for only integer 10 numbers?

SunArc Technologies, Pawan Sharma
on 11/7/17, 1:07 AM

Hello Pawan, For that you can make use of your regex like this,

import re

pattern = re.compile("^[0-9]{10}$")

if pattern.match(string):

return true

else:

return false

Parth Patel (pat)
on 11/7/17, 1:22 AM

Thanks parth work with following code:

@api.multi

@api.constrains('whats_app')

def _onchange_phone_number(self):

pattern = re.compile("^[0-9]{10}$")

for rec in self:

if pattern.match(rec.whats_app) and len(str(rec.whats_app)) == 10:

return True

else:

raise ValidationError(_("Invalid Mobile No."))

SunArc Technologies, Pawan Sharma
on 11/7/17, 4:00 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 11/6/17, 2:21 AM
Seen: 1513 times
Last updated: 11/7/17, 1:07 AM