تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
23043 أدوات العرض

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.

الصورة الرمزية
إهمال
أفضل إجابة

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']), ]
الصورة الرمزية
إهمال
أفضل إجابة

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

الكاتب أفضل إجابة

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

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
0
مارس 15
3364
4
مارس 15
10272
1
مارس 15
5004
0
مارس 15
5530
3
أكتوبر 23
34415