Ticket #8527: ca-form.diff
File ca-form.diff, 2.7 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/ca/forms.py
3 3 """ 4 4 5 5 from django.forms import ValidationError 6 from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES6 from django.forms.fields import Field, CharField, Select, EMPTY_VALUES 7 7 from django.forms.util import smart_unicode 8 8 from django.utils.translation import ugettext_lazy as _ 9 9 import re … … 11 11 phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$') 12 12 sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$") 13 13 14 class CAPostalCodeField( RegexField):14 class CAPostalCodeField(CharField): 15 15 """Canadian postal code field.""" 16 16 default_error_messages = { 17 17 'invalid': _(u'Enter a postal code in the format XXX XXX.'), 18 18 } 19 19 20 def __init__(self, *args, **kwargs): 21 super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXYZ]\d[A-Z] \d[A-Z]\d$', 22 max_length=None, min_length=None, *args, **kwargs) 20 postcode_regex = re.compile(r'^([ABCEGHJKLMNPRSTVXYZ]\d[A-Z]) *(\d[A-Z]\d)$') 23 21 22 def clean(self, value): 23 value = super(CAPostalCodeField, self).clean(value) 24 if value == u'': 25 return value 26 postcode = value.upper().strip() 27 m = self.postcode_regex.match(postcode) 28 if not m: 29 raise ValidationError(self.default_error_messages['invalid']) 30 31 return "%s %s" % (m.group(1), m.group(2)) 32 24 33 class CAPhoneNumberField(Field): 25 34 """Canadian phone number field.""" 26 35 default_error_messages = { -
tests/regressiontests/forms/localflavor/ca.py
37 37 >>> f.clean('T2S 2H7') 38 38 u'T2S 2H7' 39 39 >>> f.clean('T2S2H7') 40 Traceback (most recent call last): 41 ... 42 ValidationError: [u'Enter a postal code in the format XXX XXX.'] 40 u'T2S 2H7' 43 41 >>> f.clean('T2S 2H') 44 42 Traceback (most recent call last): 45 43 ... … … 60 58 u'' 61 59 >>> f.clean('') 62 60 u'' 61 >>> f.clean('t2s h8i') 62 Traceback (most recent call last): 63 ... 64 ValidationError: [u'Enter a postal code in the format XXX XXX.'] 65 >>> f.clean('t2s 2h7') 66 u'T2S 2H7' 67 >>> f.clean('t2s2h7') 68 u'T2S 2H7' 69 >>> f.clean('t2s2H7') 70 u'T2S 2H7' 71 >>> f.clean('t2s 2H7') 72 u'T2S 2H7' 73 >>> f.clean(' t2s 2H7') 74 u'T2S 2H7' 75 >>> f.clean(' t2s 2H7 ') 76 u'T2S 2H7' 77 >>> f.clean(' T2S 2H7 ') 78 u'T2S 2H7' 63 79 64 80 # CAPhoneNumberField ########################################################## 65 81