Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odpovědi
22967 Zobrazení

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.

Avatar
Zrušit
Nejlepší odpověď

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']), ]
Avatar
Zrušit
Nejlepší odpověď

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.

Avatar
Zrušit
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 Nejlepší odpověď

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

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
0
bře 15
3315
4
bře 15
10226
1
bře 15
4958
0
bře 15
5455
3
říj 23
34361