Odoo Help

Welcome!

This community 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.

0

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

By
Lince
on 4/18/16, 6:34 PM 132 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

1
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')

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

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

Yenthe
on 4/19/16, 2:03 AM
0
Lince
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 ?

Lince
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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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