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

Allow only numeric values in a field.char

By
Ronald
on 5/5/14, 8:03 PM 3,392 views

Hi and thanks for reading this post.

I have defined a custom field:

'x_dni': fields.char('DNI.', size=13, required=True)

As you can see, it is a char and what I need to achieve is that only numeric values can be entered in the view.

Is there a way to control the values entered in the field so that only numeric values are allowed. I am using OpenErp V7

Thanks again for reading.

1

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3918
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 5/7/14, 4:50 AM

I had a similar work with phone number validation with regular expression.

import re

def is_phone(self, cr, uid, ids, context=None):
        record = self.browse(cr, uid, ids)
        pattern ="^[0-9]{10}$"
        for data in record:
            if re.match(pattern, data.phone):
                return True
            else:
                return False
        return {}

    _constraints = [(is_phone, 'Error: Invalid phone', ['phone']), ]
0

Nehal

--Nehal--
2090
| 5 2 8
Ahmadabad, India
--Nehal--
Nehal
On 5/6/14, 1:50 AM

Hi,

You can apply integer or float type for the field instead of char, if you want always integer value in this field. Or you can apply _constraints to check the value of that field.

For example:

def _check_value(self, cr, uid, ids, context=None):        
    for val in self.browse(cr, uid, ids, context=context):
        if val.phone and isinstance(val.phone, int):
            return True
    return False

_constraints = [
    (_check_value, 'You cannot add value other than integer".', ['phone']),
]

This will not allow you to save record until you enter integer value in phone field.

You can also apply onchange method if you want to check the value before saving record. You just need to apply on_change method in that field of xml file and the function in py file.

hope this may help you.

Thanks for you replay I appreciate it @Nehal. I could use integers or floats in the field, but the thing is that I must allow leading zeros, so the user can input let's say

0001231999111 0128801231001

That is why I use char.

I will try you suggestions and see if they can fit my needs. I will let you know.

Thanks again for your reply

Ronald
on 5/6/14, 12:14 PM
0
Ronald
On 5/6/14, 12:13 PM

Thanks for you replay I appreciate it @Nehal. I could use integers or floats in the field, but the thing is that I must allow leading zeros, so the user can input let's say

0001231999111
0128801231001

That is why I use char.

I will try you suggestions and see if they can fit my needs. I will let you know.

Thanks again for your reply

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: 5/5/14, 8:03 PM
Seen: 3392 times
Last updated: 3/16/15, 8:10 AM