Author: adrian
Date: 2007-02-14 21:37:25 -0600 (Wed, 14 Feb 2007)
New Revision: 4519

Added:
   django/trunk/django/newforms/extras/usa.py
Modified:
   django/trunk/tests/regressiontests/forms/tests.py
Log:
Fixed #1466 -- Added newforms USZipCodeField

Added: django/trunk/django/newforms/extras/usa.py
===================================================================
--- django/trunk/django/newforms/extras/usa.py                          (rev 0)
+++ django/trunk/django/newforms/extras/usa.py  2007-02-15 03:37:25 UTC (rev 
4519)
@@ -0,0 +1,13 @@
+"""
+USA-specific Form helpers
+"""
+
+from django.newforms.fields import RegexField
+from django.utils.translation import gettext
+
+class USZipCodeField(RegexField):
+    def __init__(self, *args, **kwargs):
+        super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$',
+            max_length=None, min_length=None,
+            error_message=gettext(u'Enter a zip code in the format XXXXX or 
XXXXX-XXXX.'),
+            *args, **kwargs)

Modified: django/trunk/tests/regressiontests/forms/tests.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests.py   2007-02-15 00:21:32 UTC 
(rev 4518)
+++ django/trunk/tests/regressiontests/forms/tests.py   2007-02-15 03:37:25 UTC 
(rev 4519)
@@ -3055,12 +3055,12 @@
 <input type="submit" />
 </form>
 
-#################
-# Extra widgets #
-#################
+###############
+# Extra stuff #
+###############
 
-The newforms library comes with some extra, higher-level Widget classes that
-demonstrate some of the library's abilities.
+The newforms library comes with some extra, higher-level Field and Widget
+classes that demonstrate some of the library's abilities.
 
 # SelectDateWidget ############################################################
 
@@ -3189,6 +3189,66 @@
 <option value="2016">2016</option>
 </select>
 
+# USZipCodeField ##############################################################
+
+USZipCodeField validates that the data is either a five-digit U.S. zip code or
+a zip+4.
+>>> from django.newforms.extras.usa import USZipCodeField
+>>> f = USZipCodeField()
+>>> f.clean('60606')
+u'60606'
+>>> f.clean(60606)
+u'60606'
+>>> f.clean('04000')
+u'04000'
+>>> f.clean('4000')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
+>>> f.clean('60606-1234')
+u'60606-1234'
+>>> f.clean('6060-1234')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
+>>> f.clean('60606-')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
+>>> 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 = USZipCodeField(required=False)
+>>> f.clean('60606')
+u'60606'
+>>> f.clean(60606)
+u'60606'
+>>> f.clean('04000')
+u'04000'
+>>> f.clean('4000')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
+>>> f.clean('60606-1234')
+u'60606-1234'
+>>> f.clean('6060-1234')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
+>>> f.clean('60606-')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
+>>> f.clean(None)
+u''
+>>> f.clean('')
+u''
 """
 
 if __name__ == "__main__":


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to