Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5670 -- Changed the validation of the UK postcode widget to al…

…low for easier input. Normalisation still returns the same things as previously. Patch from scott@staplefish.com.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6952 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a35ca605d62d1b97eb595dcaa27b0904a0fe2182 1 parent aec9664
Malcolm Tredinnick authored December 19, 2007
28  django/contrib/localflavor/uk/forms.py
@@ -2,23 +2,39 @@
2 2
 UK-specific Form helpers
3 3
 """
4 4
 
5  
-from django.newforms.fields import RegexField, Select
  5
+import re
  6
+
  7
+from django.newforms.fields import CharField, Select
  8
+from django.newforms import ValidationError
6 9
 from django.utils.translation import ugettext
7 10
 
8  
-class UKPostcodeField(RegexField):
  11
+class UKPostcodeField(CharField):
9 12
     """
10 13
     A form field that validates its input is a UK postcode.
11 14
 
12 15
     The regular expression used is sourced from the schema for British Standard
13 16
     BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd
  17
+
  18
+    The value is uppercased and a space added in the correct place, if required.
14 19
     """
15 20
     default_error_messages = {
16  
-        'invalid': ugettext(u'Enter a postcode. A space is required between the two postcode parts.'),
  21
+        'invalid': ugettext(u'Enter a valid postcode.'),
17 22
     }
  23
+    outcode_pattern = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])'
  24
+    incode_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}'
  25
+    postcode_regex = re.compile(r'^(GIR 0AA|%s %s)$' % (outcode_pattern, incode_pattern))
  26
+    space_regex = re.compile(r' *(%s)$' % incode_pattern)
18 27
 
19  
-    def __init__(self, *args, **kwargs):
20  
-        super(UKPostcodeField, self).__init__(r'^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW]) [0-9][ABD-HJLNP-UW-Z]{2})$',
21  
-            max_length=None, min_length=None, *args, **kwargs)
  28
+    def clean(self, value):
  29
+        value = super(UKPostcodeField, self).clean(value)
  30
+        if value == u'':
  31
+            return value
  32
+        postcode = value.upper().strip()
  33
+        # Put a single space before the incode (second part).
  34
+        postcode = self.space_regex.sub(r' \1', postcode)
  35
+        if not self.postcode_regex.search(postcode):
  36
+            raise ValidationError(self.default_error_messages['invalid'])
  37
+        return postcode
22 38
 
23 39
 class UKCountySelect(Select):
24 40
     """
29  tests/regressiontests/forms/localflavor/uk.py
@@ -12,13 +12,15 @@
12 12
 >>> f.clean('GIR 0AA')
13 13
 u'GIR 0AA'
14 14
 >>> f.clean('BT324PX')
  15
+u'BT32 4PX'
  16
+>>> f.clean('1NV 4L1D')
15 17
 Traceback (most recent call last):
16 18
 ...
17  
-ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
18  
->>> f.clean('1NV 4L1D')
  19
+ValidationError: [u'Enter a valid postcode.']
  20
+>>> f.clean('1NV4L1D')
19 21
 Traceback (most recent call last):
20 22
 ...
21  
-ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
  23
+ValidationError: [u'Enter a valid postcode.']
22 24
 >>> f.clean(None)
23 25
 Traceback (most recent call last):
24 26
 ...
@@ -27,7 +29,20 @@
27 29
 Traceback (most recent call last):
28 30
 ...
29 31
 ValidationError: [u'This field is required.']
30  
-
  32
+>>> f.clean(' so11aa ')
  33
+u'SO1 1AA'
  34
+>>> f.clean(' so1  1aa ')
  35
+u'SO1 1AA'
  36
+>>> f.clean('G2 3wt')
  37
+u'G2 3WT'
  38
+>>> f.clean('EC1A 1BB')
  39
+u'EC1A 1BB'
  40
+>>> f.clean('Ec1a1BB')
  41
+u'EC1A 1BB'
  42
+>>> f.clean(' b0gUS')
  43
+Traceback (most recent call last):
  44
+...
  45
+ValidationError: [u'Enter a valid postcode.']
31 46
 >>> f = UKPostcodeField(required=False)
32 47
 >>> f.clean('BT32 4PX')
33 48
 u'BT32 4PX'
@@ -36,11 +51,9 @@
36 51
 >>> f.clean('1NV 4L1D')
37 52
 Traceback (most recent call last):
38 53
 ...
39  
-ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
  54
+ValidationError: [u'Enter a valid postcode.']
40 55
 >>> f.clean('BT324PX')
41  
-Traceback (most recent call last):
42  
-...
43  
-ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
  56
+u'BT32 4PX'
44 57
 >>> f.clean(None)
45 58
 u''
46 59
 >>> f.clean('')

0 notes on commit a35ca60

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