Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
23271 Widoki

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.

Awatar
Odrzuć
Najlepsza odpowiedź

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']), ]
Awatar
Odrzuć
Najlepsza odpowiedź

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.

Awatar
Odrzuć
Autor

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

Autor Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
mar 15
3459
4
mar 15
10382
1
mar 15
5133
0
mar 15
5650
3
paź 23
34610