Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with following code in Python?

I was trying to implement a constraint for a field but instead of causing a constraint validation, it allows the record to get saved without showing any constraint message

def _check_contact_number(self, cr, uid, ids, context=None):
    for rec in self.browse(cr, uid, ids, context=context):
                if rec.contact_number:
                    size=len(str(rec.contact_number))
                    if size<10:
                       return False
            if not contact_number:
            return {}
            contact_number = rec.contact_number.replace('.','') 
#removes any '.' from the string
            contact_number = rec.contact_number.replace(' ','') 
#removes space from the string
            if not  contact_number.isdigit():
            return False
        return {}

    _constraints = [
        (_check_contact_number, 'Enter valid phone number...!', 
['contact_number']),
      ]

Kindly anyone correct me. Thank you very much

like image 262
Shravy Avatar asked May 13 '26 19:05

Shravy


1 Answers

This code has an ugly indents. Maybe this is the reason. The right idents looks so:

def _check_contact_number(self, cr, uid, ids, context=None):
    for rec in self.browse(cr, uid, ids, context=context):
        if rec.contact_number:
            size=len(str(rec.contact_number))
            if size<10:
                return False
        if not contact_number:
            return {}
            contact_number = rec.contact_number.replace('.','') 
#removes any '.' from the string
            contact_number = rec.contact_number.replace(' ','') 
#removes space from the string
        if not  contact_number.isdigit():
            return False
        return {}
    _constraints = [
        (_check_contact_number, 'Enter valid phone number...!', ['contact_number']),
      ]
like image 96
Sdwdaw Avatar answered May 16 '26 07:05

Sdwdaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!