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

Regular expression and empty string in Odoo

By
Julius
on 1/11/16, 10:35 AM 1,234 views

Hi all,

i'm testing some validation functions for the phone field to avoid others characters than numbers from 0 to 9.

    def valide_tel(self, cr, uid, ids, context=None):
        record = self.browse(cr, uid, ids)
        pattern = "^\+?[0-9]*$"
        for data in record:
            if re.match(pattern, data.phone) == None:
                return False
        return True



I tested the regex on pythex.org with success, but the problem is that when no phone number is entered, Odoo complains about ValidationError: expected string or buffer.

What can i do to have this working?

Thanks

2

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 1/12/16, 12:52 AM

Julius,

When there is no value entered in the phone field, it will take it as False, and regular expression will work on string only.

So, either you make your phone field as mandatory to the user or before implementing 'if condition' check if the value of data.phone is not False .

Hope this will help you!    


 

I added the condition and it works: if (data.phone != False and re.match(pattern, data.phone) == None): Thank you Pawan !

iMaugis, Julius
on 1/12/16, 9:07 AM

:)

Pawan
on 1/12/16, 10:30 AM
1
Swapnesh
On 1/11/16, 1:18 PM

Hello, 

You will find  \this\\ and\&nbsp\;\this Helpful.


Thanks

http://stackoverflow.com/questions/16135069/python-validation-mobile-number and http://stackoverflow.com/questions/16125964/python-trying-to-validate-input-such-as-a-phone-number-to-ensure-it-is-10-numer i am not able to post link in answer (may due to bug) Thanks

Swapnesh
on 1/11/16, 1:22 PM

It works when you paste the link twice into the form :-)

Ermin Trevisan
on 1/12/16, 9:31 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: 1/11/16, 10:35 AM
Seen: 1234 times
Last updated: 1/12/16, 9:04 AM