Ticket #8934: localflavor.ar.ARPhoneNumberField.diff

File localflavor.ar.ARPhoneNumberField.diff, 1.2 KB (added by fperetti, 7 years ago)
  • django/contrib/localflavor/ar/forms.py

     
    112112            cuit = cuit[:-1]
    113113        return u'%s-%s-%s' % (cuit[:2], cuit[2:], check_digit)
    114114
     115class ARPhoneNumberField(RegexField):
     116    """
     117    Argentina phone numbers field.
     118    """
     119    default_error_messages = {
     120        'invalid': _('Phone numbers must be in XXXX-XXXXXX format.'),
     121    }
     122
     123    def __init__(self, *args, **kwargs):
     124        super(ARPhoneNumberField, self).__init__(r'^[0-9\-\(\)\s]{6,16}$',
     125                max_length=16, min_length=6, *args, **kwargs)
     126
     127    def clean(self, value):
     128        """
     129        Strips -, (, ) and spaces. Checks the final length.
     130        """
     131        value = super(ARPhoneNumberField, self).clean(value)
     132        value = value.replace('-','')
     133        value = value.replace('(','')
     134        value = value.replace(')','')
     135        value = value.replace(' ','')
     136        if len(value) != 10:
     137            raise ValidationError(self.error_messages['invalid'])
     138        return value
Back to Top