mirror of
https://github.com/django/django.git
synced 2024-12-01 15:42:04 +01:00
Fixed #3503 -- Added newforms UKPostcodeField in django.contrib.localflavor.uk. Thanks for the patch, Jonathan Buchanan
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4527 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c96d155589
commit
1d5e974a4f
0
django/contrib/localflavor/uk/__init__.py
Normal file
0
django/contrib/localflavor/uk/__init__.py
Normal file
19
django/contrib/localflavor/uk/forms.py
Normal file
19
django/contrib/localflavor/uk/forms.py
Normal file
@ -0,0 +1,19 @@
|
||||
"""
|
||||
UK-specific Form helpers
|
||||
"""
|
||||
|
||||
from django.newforms.fields import RegexField
|
||||
from django.utils.translation import gettext
|
||||
|
||||
class UKPostcodeField(RegexField):
|
||||
"""
|
||||
A form field that validates its input is a UK postcode.
|
||||
|
||||
The regular expression used is sourced from the schema for British Standard
|
||||
BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
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})$',
|
||||
max_length=None, min_length=None,
|
||||
error_message=gettext(u'Enter a postcode. A space is required between the two postcode parts.'),
|
||||
*args, **kwargs)
|
@ -3327,6 +3327,50 @@ u''
|
||||
>>> f.clean('')
|
||||
u''
|
||||
|
||||
# UKPostcodeField ##############################################################
|
||||
|
||||
UKPostcodeField validates that the data is a valid UK postcode.
|
||||
>>> from django.contrib.localflavor.uk.forms import UKPostcodeField
|
||||
>>> f = UKPostcodeField()
|
||||
>>> f.clean('BT32 4PX')
|
||||
u'BT32 4PX'
|
||||
>>> f.clean('GIR 0AA')
|
||||
u'GIR 0AA'
|
||||
>>> f.clean('BT324PX')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
|
||||
>>> f.clean('1NV 4L1D')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
|
||||
>>> f.clean(None)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'This field is required.']
|
||||
>>> f.clean('')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'This field is required.']
|
||||
|
||||
>>> f = UKPostcodeField(required=False)
|
||||
>>> f.clean('BT32 4PX')
|
||||
u'BT32 4PX'
|
||||
>>> f.clean('GIR 0AA')
|
||||
u'GIR 0AA'
|
||||
>>> f.clean('1NV 4L1D')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
|
||||
>>> f.clean('BT324PX')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
|
||||
>>> f.clean(None)
|
||||
u''
|
||||
>>> f.clean('')
|
||||
u''
|
||||
|
||||
#################################
|
||||
# Tests of underlying functions #
|
||||
#################################
|
||||
|
Loading…
Reference in New Issue
Block a user