Django

Code

Show
Ignore:
Timestamp:
12/17/07 02:05:27 (1 year ago)
Author:
mtredinnick
Message:

Fixed #5871 -- Factored out the validation errors in localflavor form fields. Brings them into line with the standard newforms fields. Patch from Jan Rademaker.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/localflavor/nl/forms.py

    r6139 r6926  
    1818    A Dutch postal code field. 
    1919    """ 
     20    default_error_messages = { 
     21        'invalid': _('Enter a valid postal code'), 
     22    } 
     23 
    2024    def clean(self, value): 
    2125        super(NLZipCodeField, self).clean(value) 
    2226        if value in EMPTY_VALUES: 
    2327            return u'' 
    24          
    25         msg = _('Enter a valid postal code') 
     28 
    2629        value = value.strip().upper().replace(' ', '') 
    2730        if not pc_re.search(value): 
    28             raise ValidationError(msg
    29          
     31            raise ValidationError(self.error_messages['invalid']
     32 
    3033        if int(value[:4]) < 1000: 
    31             raise ValidationError(msg
    32          
     34            raise ValidationError(self.error_messages['invalid']
     35 
    3336        return u'%s %s' % (value[:4], value[4:]) 
    3437 
    3538class NLProvinceSelect(Select): 
    3639    """ 
    37     A Select widget that uses a list of provinces of the Netherlands as its  
     40    A Select widget that uses a list of provinces of the Netherlands as its 
    3841    choices. 
    3942    """ 
     
    4649    A Dutch telephone number field. 
    4750    """ 
     51    default_error_messages = { 
     52        'invalid': _('Enter a valid phone number'), 
     53    } 
     54 
    4855    def clean(self, value): 
    4956        super(NLPhoneNumberField, self).clean(value) 
    5057        if value in EMPTY_VALUES: 
    5158            return u'' 
    52          
    53         msg = _('Enter a valid phone number') 
     59 
    5460        phone_nr = re.sub('[\-\s\(\)]', '', smart_unicode(value)) 
    55          
     61 
    5662        if len(phone_nr) == 10 and numeric_re.search(phone_nr): 
    5763            return value 
    58          
     64 
    5965        if phone_nr[:3] == '+31' and len(phone_nr) == 12 and \ 
    6066           numeric_re.search(phone_nr[3:]): 
    6167            return value 
    62          
    63         raise ValidationError(msg
     68 
     69        raise ValidationError(self.error_messages['invalid']
    6470 
    6571class NLSoFiNumberField(Field): 
    6672    """ 
    6773    A Dutch social security number (SoFi/BSN) field. 
    68   
     74 
    6975    http://nl.wikipedia.org/wiki/Sofinummer 
    7076    """ 
     77    default_error_messages = { 
     78        'invalid': _('Enter a valid SoFi number'), 
     79    } 
     80 
    7181    def clean(self, value): 
    7282        super(NLSoFiNumberField, self).clean(value) 
    7383        if value in EMPTY_VALUES: 
    7484            return u'' 
    75          
    76         msg = _('Enter a valid SoFi number') 
    77          
     85 
    7886        if not sofi_re.search(value): 
    79             raise ValidationError(msg
    80          
     87            raise ValidationError(self.error_messages['invalid']
     88 
    8189        if int(value) == 0: 
    82             raise ValidationError(msg
    83          
     90            raise ValidationError(self.error_messages['invalid']
     91 
    8492        checksum = 0 
    8593        for i in range(9, 1, -1): 
    8694            checksum += int(value[9-i]) * i 
    8795        checksum -= int(value[-1]) 
    88          
     96 
    8997        if checksum % 11 != 0: 
    90             raise ValidationError(msg
    91          
     98            raise ValidationError(self.error_messages['invalid']
     99 
    92100        return value