跳至內容
選單
此問題已被標幟
3 回覆
23285 瀏覽次數

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
3月 15
3461
4
3月 15
10394
1
3月 15
5141
0
3月 15
5676
3
10月 23
34618