Ticket #1466: zip.diff

File zip.diff, 3.6 KB (added by erikankrom@…, 18 years ago)

USZipCodeField Enhancement/Documentation

  • django/db/models/fields/__init__.py

     
    689689    def validate(self, field_data, all_data):
    690690        validators.isValidPhone(field_data, all_data)
    691691
     692class USZipCodeField(Field):
     693    def get_manipulator_field_objs(self):
     694        return [forms.USZipCodeField]
     695
     696    def validate(self, field_data, all_data):
     697        validators.isValidUSZipCode(field_data, all_data)
     698
    692699class PositiveIntegerField(IntegerField):
    693700    def get_manipulator_field_objs(self):
    694701        return [forms.PositiveIntegerField]
  • django/forms/__init__.py

     
    937937        except validators.ValidationError, e:
    938938            raise validators.CriticalValidationError, e.messages
    939939
     940class USZipCodeField(TextField):
     941    "A convenience FormField for validating US Zip Codes (e.g. '12345 or 12345-1234')"
     942    def __init__(self, field_name, is_required=False, validator_list=[]):
     943        validator_list = [self.isValidUSZipCode] + validator_list
     944        TextField.__init__(self, field_name, length=10, maxlength=10,
     945            is_required=is_required, validator_list=validator_list)
     946
     947    def isValidUSZipCode(self, field_data, all_data):
     948        try:
     949            validators.isValidUSZipCode(field_data, all_data)
     950        except validators.ValidationError, e:
     951            raise validators.CriticalValidationError, e.messages
     952
    940953class USStateField(TextField):
    941954    "A convenience FormField for validating U.S. states (e.g. 'IL')"
    942955    def __init__(self, field_name, is_required=False, validator_list=None):
  • django/core/validators.py

     
    2929phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
    3030slug_re = re.compile(r'^[-\w]+$')
    3131url_re = re.compile(r'^https?://\S+$')
     32uszipcode_re = re.compile(r'^(\d{5}$)|(\d{5}-\d{4}$)')
    3233
    3334lazy_inter = lazy(lambda a,b: str(a) % b, str)
    3435
     
    220221    if field_data.upper() not in states:
    221222        raise ValidationError, gettext("Enter a valid U.S. state abbreviation.")
    222223
     224def isValidUSZipCode(field_data, all_data):
     225    if not uszipcode_re.search(field_data):
     226        raise ValidationError, gettext('Zip Codes must be in XXXXX or XXXXX-XXXX format. "%s" is invalid.') % field_data
     227
    223228def hasNoProfanities(field_data, all_data):
    224229    """
    225230    Checks that the given string has no profanities in it. This does a simple
  • docs/forms.txt

     
    2929        address = models.CharField(maxlength=100, blank=True)
    3030        city = models.CharField(maxlength=50, blank=True)
    3131        state = models.USStateField()
    32         zip_code = models.CharField(maxlength=5, blank=True)
     32        zip_code = models.USZipCodeField()
    3333        place_type = models.IntegerField(choices=PLACE_TYPES)
    3434
    3535        class Admin:
     
    518518    * isWellFormedXmlFragment
    519519    * isExistingURL
    520520    * isValidUSState
     521    * isValidUSZipCode
    521522    * hasNoProfanities
    522523
    523524There are also a group of validators that are slightly more flexible. For
Back to Top