Ticket #3368: newforms-new-fields.diff

File newforms-new-fields.diff, 3.9 KB (added by jkocherhans, 18 years ago)
  • django/newforms/fields.py

     
    1515    'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
    1616    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
    1717    'RegexField', 'EmailField', 'URLField', 'BooleanField',
     18    'USStateField', 'PhoneNumberField',
    1819    'ChoiceField', 'NullBooleanField', 'MultipleChoiceField',
    1920    'ComboField', 'MultiValueField',
    2021    'SplitDateTimeField',
     
    266267    def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None):
    267268        RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label, initial)
    268269
     270phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
     271
     272class PhoneNumberField(RegexField):
     273    def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None):
     274        super(PhoneNumberField, self).__init__(phone_re, max_length, min_length, u'Phone numbers must be in XXX-XXX-XXXX format.', required, widget, label, initial)
     275
     276class USStateField(CharField):
     277    def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None):
     278        super(USStateField, self).__init__(max_length, min_length, required, widget, label, initial)
     279
     280    def clean(self, value):
     281        value = super(USStateField, self).clean(value)
     282        if value == u'':
     283            return value
     284        # always store as upper case
     285        value = value.upper()
     286        states = ['AA', 'AE', 'AK', 'AL', 'AP', 'AR', 'AS', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'FM', 'GA', 'GU', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MH', 'MI', 'MN', 'MO', 'MP', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'PW', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI', 'WV', 'WY']
     287        if value not in states:
     288            raise ValidationError(u'Enter a valid U.S. state abbreviation.')
     289        return value
     290
    269291url_re = re.compile(
    270292    r'^https?://' # http:// or https://
    271293    r'(?:[A-Z0-9-]+\.)+[A-Z]{2,6}' # domain
  • tests/regressiontests/forms/tests.py

     
    13071307...
    13081308ValidationError: [u'Ensure this value has at most 15 characters.']
    13091309
     1310# PhoneNumberField ###########################################################
     1311
     1312>>> f = PhoneNumberField()
     1313>>> f.clean('')
     1314Traceback (most recent call last):
     1315...
     1316ValidationError: [u'This field is required.']
     1317>>> f.clean(None)
     1318Traceback (most recent call last):
     1319...
     1320ValidationError: [u'This field is required.']
     1321>>> f.clean('555-555-5555')
     1322u'555-555-5555'
     1323>>> f.clean('foo')
     1324Traceback (most recent call last):
     1325...
     1326ValidationError: [u'Phone numbers must be in XXX-XXX-XXXX format.']
     1327
     1328>>> f = PhoneNumberField(required=False)
     1329>>> f.clean('')
     1330u''
     1331>>> f.clean(None)
     1332u''
     1333
     1334# USStatefield ###############################################################
     1335
     1336>>> f = USStateField()
     1337>>> f.clean('')
     1338Traceback (most recent call last):
     1339...
     1340ValidationError: [u'This field is required.']
     1341>>> f.clean(None)
     1342Traceback (most recent call last):
     1343...
     1344ValidationError: [u'This field is required.']
     1345>>> f.clean('KS')
     1346u'KS'
     1347
     1348USStateField always converts to upper case
     1349>>> f.clean('ks')
     1350u'KS'
     1351>>> f.clean('YY')
     1352Traceback (most recent call last):
     1353...
     1354ValidationError: [u'Enter a valid U.S. state abbreviation.']
     1355
     1356>>> f = USStateField(required=False)
     1357>>> f.clean('')
     1358u''
     1359>>> f.clean(None)
     1360u''
     1361
    13101362# URLField ##################################################################
    13111363
    13121364>>> f = URLField()
Back to Top