Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8527 -- Made CAPostalCodeField more forgiving of the input for…

…mat. Thanks to Claude Paroz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16175 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 02b837d38a37239dcd7366e7735e12aff51ab790 1 parent c5f58f5
Jannis Leidel authored May 07, 2011
18  django/contrib/localflavor/ca/forms.py
@@ -4,7 +4,7 @@
4 4
 
5 5
 from django.core.validators import EMPTY_VALUES
6 6
 from django.forms import ValidationError
7  
-from django.forms.fields import Field, RegexField, Select
  7
+from django.forms.fields import Field, CharField, Select
8 8
 from django.utils.encoding import smart_unicode
9 9
 from django.utils.translation import ugettext_lazy as _
10 10
 import re
@@ -12,7 +12,7 @@
12 12
 phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
13 13
 sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$")
14 14
 
15  
-class CAPostalCodeField(RegexField):
  15
+class CAPostalCodeField(CharField):
16 16
     """
17 17
     Canadian postal code field.
18 18
 
@@ -25,9 +25,17 @@ class CAPostalCodeField(RegexField):
25 25
         'invalid': _(u'Enter a postal code in the format XXX XXX.'),
26 26
     }
27 27
 
28  
-    def __init__(self, max_length=None, min_length=None, *args, **kwargs):
29  
-        super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$',
30  
-            max_length, min_length, *args, **kwargs)
  28
+    postcode_regex = re.compile(r'^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]) *(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$')
  29
+
  30
+    def clean(self, value):
  31
+        value = super(CAPostalCodeField, self).clean(value)
  32
+        if value in EMPTY_VALUES:
  33
+            return u''
  34
+        postcode = value.upper().strip()
  35
+        m = self.postcode_regex.match(postcode)
  36
+        if not m:
  37
+            raise ValidationError(self.default_error_messages['invalid'])
  38
+        return "%s %s" % (m.group(1), m.group(2))
31 39
 
32 40
 class CAPhoneNumberField(Field):
33 41
     """Canadian phone number field."""
8  tests/regressiontests/forms/localflavor/ca.py
@@ -45,13 +45,17 @@ def test_CAPostalCodeField(self):
45 45
             'T2S 2W7': 'T2S 2W7',
46 46
             'T2S 2Z7': 'T2S 2Z7',
47 47
             'T2Z 2H7': 'T2Z 2H7',
48  
-
  48
+            'T2S2H7' : 'T2S 2H7',
  49
+            't2s 2h7': 'T2S 2H7',
  50
+            't2s2h7' : 'T2S 2H7',
  51
+            't2s            2H7': 'T2S 2H7',
  52
+            '  t2s    2H7  ': 'T2S 2H7',
49 53
         }
50 54
         invalid = {
51  
-            'T2S2H7' : error_format,
52 55
             'T2S 2H' : error_format,
53 56
             '2T6 H8I': error_format,
54 57
             'T2S2H' : error_format,
  58
+            't2s h8i': error_format,
55 59
             90210 : error_format,
56 60
             'W2S 2H3': error_format,
57 61
             'Z2S 2H3': error_format,

0 notes on commit 02b837d

Please sign in to comment.
Something went wrong with that request. Please try again.