Skip to Content
Menu
This question has been flagged
3 Replies
3049 Views

Hello,

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

Avatar
Discard
Best Answer

you can try this:


from openerp.exceptions import UserError, RedirectWarning, ValidationError

@api.constrains('YOUR FIELD NAME')

@api.one

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.

Avatar
Discard

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

Author Best Answer

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 ?

Avatar
Discard
Author

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

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