Ticket #3503: ukpostcodefield.diff

File ukpostcodefield.diff, 2.9 KB (added by Jonathan Buchanan <jonathan.buchanan@…, 14 years ago)

UKPostcodeField patch

  • django/contrib/localflavor/uk/forms.py

     
     1"""
     2UK-specific Form helpers
     3"""
     4
     5from django.newforms.fields import RegexField
     6from django.utils.translation import gettext
     7
     8class UKPostcodeField(RegexField):
     9    """
     10    A form field that validates its input is a UK postcode.
     11
     12    The regular expression used is sourced from the schema for British Standard
     13    BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd
     14    """
     15    def __init__(self, *args, **kwargs):
     16        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})$',
     17            max_length=None, min_length=None,
     18            error_message=gettext(u'Enter a postcode. A space is required between the two postcode parts.'),
     19            *args, **kwargs)
     20 No newline at end of file
  • tests/regressiontests/forms/tests.py

     
    33273327>>> f.clean('')
    33283328u''
    33293329
     3330# UKPostcodeField ##############################################################
     3331
     3332UKPostcodeField validates that the data is a valid UK postcode.
     3333>>> from django.contrib.localflavor.uk.forms import UKPostcodeField
     3334>>> f = UKPostcodeField()
     3335>>> f.clean('BT32 4PX')
     3336u'BT32 4PX'
     3337>>> f.clean('GIR 0AA')
     3338u'GIR 0AA'
     3339>>> f.clean('BT324PX')
     3340Traceback (most recent call last):
     3341...
     3342ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
     3343>>> f.clean('1NV 4L1D')
     3344Traceback (most recent call last):
     3345...
     3346ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
     3347>>> f.clean(None)
     3348Traceback (most recent call last):
     3349...
     3350ValidationError: [u'This field is required.']
     3351>>> f.clean('')
     3352Traceback (most recent call last):
     3353...
     3354ValidationError: [u'This field is required.']
     3355
     3356>>> f = UKPostcodeField(required=False)
     3357>>> f.clean('BT32 4PX')
     3358u'BT32 4PX'
     3359>>> f.clean('GIR 0AA')
     3360u'GIR 0AA'
     3361>>> f.clean('1NV 4L1D')
     3362Traceback (most recent call last):
     3363...
     3364ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
     3365>>> f.clean('BT324PX')
     3366Traceback (most recent call last):
     3367...
     3368ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
     3369>>> f.clean(None)
     3370u''
     3371>>> f.clean('')
     3372u''
     3373
    33303374#################################
    33313375# Tests of underlying functions #
    33323376#################################
Back to Top