Author: adrian Date: 2007-02-15 15:26:43 -0600 (Thu, 15 Feb 2007) New Revision: 4527
Added: django/trunk/django/contrib/localflavor/uk/ django/trunk/django/contrib/localflavor/uk/__init__.py django/trunk/django/contrib/localflavor/uk/forms.py Modified: django/trunk/tests/regressiontests/forms/tests.py Log: Fixed #3503 -- Added newforms UKPostcodeField in django.contrib.localflavor.uk. Thanks for the patch, Jonathan Buchanan Added: django/trunk/django/contrib/localflavor/uk/__init__.py =================================================================== Added: django/trunk/django/contrib/localflavor/uk/forms.py =================================================================== --- django/trunk/django/contrib/localflavor/uk/forms.py (rev 0) +++ django/trunk/django/contrib/localflavor/uk/forms.py 2007-02-15 21:26:43 UTC (rev 4527) @@ -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) Modified: django/trunk/tests/regressiontests/forms/tests.py =================================================================== --- django/trunk/tests/regressiontests/forms/tests.py 2007-02-15 20:55:37 UTC (rev 4526) +++ django/trunk/tests/regressiontests/forms/tests.py 2007-02-15 21:26:43 UTC (rev 4527) @@ -3327,6 +3327,50 @@ >>> 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 # ################################# --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-updates?hl=en -~----------~----~----~----~------~----~------~--~---
