Ticket #3368: newforms-new-fields.diff
File newforms-new-fields.diff, 3.9 KB (added by , 18 years ago) |
---|
-
django/newforms/fields.py
15 15 'DEFAULT_TIME_INPUT_FORMATS', 'TimeField', 16 16 'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField', 17 17 'RegexField', 'EmailField', 'URLField', 'BooleanField', 18 'USStateField', 'PhoneNumberField', 18 19 'ChoiceField', 'NullBooleanField', 'MultipleChoiceField', 19 20 'ComboField', 'MultiValueField', 20 21 'SplitDateTimeField', … … 266 267 def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None): 267 268 RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label, initial) 268 269 270 phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE) 271 272 class 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 276 class 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 269 291 url_re = re.compile( 270 292 r'^https?://' # http:// or https:// 271 293 r'(?:[A-Z0-9-]+\.)+[A-Z]{2,6}' # domain -
tests/regressiontests/forms/tests.py
1307 1307 ... 1308 1308 ValidationError: [u'Ensure this value has at most 15 characters.'] 1309 1309 1310 # PhoneNumberField ########################################################### 1311 1312 >>> f = PhoneNumberField() 1313 >>> f.clean('') 1314 Traceback (most recent call last): 1315 ... 1316 ValidationError: [u'This field is required.'] 1317 >>> f.clean(None) 1318 Traceback (most recent call last): 1319 ... 1320 ValidationError: [u'This field is required.'] 1321 >>> f.clean('555-555-5555') 1322 u'555-555-5555' 1323 >>> f.clean('foo') 1324 Traceback (most recent call last): 1325 ... 1326 ValidationError: [u'Phone numbers must be in XXX-XXX-XXXX format.'] 1327 1328 >>> f = PhoneNumberField(required=False) 1329 >>> f.clean('') 1330 u'' 1331 >>> f.clean(None) 1332 u'' 1333 1334 # USStatefield ############################################################### 1335 1336 >>> f = USStateField() 1337 >>> f.clean('') 1338 Traceback (most recent call last): 1339 ... 1340 ValidationError: [u'This field is required.'] 1341 >>> f.clean(None) 1342 Traceback (most recent call last): 1343 ... 1344 ValidationError: [u'This field is required.'] 1345 >>> f.clean('KS') 1346 u'KS' 1347 1348 USStateField always converts to upper case 1349 >>> f.clean('ks') 1350 u'KS' 1351 >>> f.clean('YY') 1352 Traceback (most recent call last): 1353 ... 1354 ValidationError: [u'Enter a valid U.S. state abbreviation.'] 1355 1356 >>> f = USStateField(required=False) 1357 >>> f.clean('') 1358 u'' 1359 >>> f.clean(None) 1360 u'' 1361 1310 1362 # URLField ################################################################## 1311 1363 1312 1364 >>> f = URLField()