Odoo Help


Prevent space in a phone number - add a constraint in Odoo9

on 4/18/16, 6:34 PM 336 views


I need to add a constraint in a phone number, like not having spaces between the number and the phone number must not be more longer than ten number.

I want to have this for example : 00412025678 and not this 004 12 02 56 78

Where and How can I add this contraint ? I use Odoo9.

Thank you for your help

Krupesh Laiya
On 4/19/16, 1:26 AM

you can try this:

from openerp.exceptions import UserError, RedirectWarning, ValidationError

@api.constrains('YOUR FIELD NAME')


def _check_phone_no(self):

    # here is your code

     if  ' '  in self.phone or len(self.phone) >= 10:

           raise ValidationError(_('Message.'))

hope this will help you.

or even better: automatically remove the spaces. self.phone.replace(' ', '')

on 4/19/16, 2:03 AM
On 4/19/16, 2:04 AM

Thank for your reply, this is what i need. But I really don't know where I can write it. Can you explain it ?
Does this code work also for odoo9 ?

Yenthe, thank you. But where can I put this code? In which table ? in which script ?

on 4/19/16, 2:07 AM

@Lince you can write this code in to your module's py file it means you have to inherit res.partner and put validation function into the inherited class.

Krupesh Laiya
on 4/19/16, 3:11 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.


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)


Asked: 4/18/16, 6:34 PM
Seen: 336 times
Last updated: 4/19/16, 2:04 AM