Ticket #6564: usstate.diff
File usstate.diff, 2.4 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
1055 1055 return [oldforms.USStateField] 1056 1056 1057 1057 def formfield(self, **kwargs): 1058 from django.contrib.localflavor.us.forms import USState Select1059 defaults = {' widget': USStateSelect}1058 from django.contrib.localflavor.us.forms import USStateField as USStateFormField 1059 defaults = {'form_class': USStateFormField} 1060 1060 defaults.update(kwargs) 1061 1061 return super(USStateField, self).formfield(**defaults) 1062 1062 -
django/contrib/localflavor/us/forms.py
3 3 """ 4 4 5 5 from django.newforms import ValidationError 6 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES 6 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES, ChoiceField 7 7 from django.utils.encoding import smart_unicode 8 8 from django.utils.translation import ugettext 9 9 import re … … 77 77 raise ValidationError(self.error_messages['invalid']) 78 78 return u'%s-%s-%s' % (area, group, serial) 79 79 80 class USStateField( Field):80 class USStateField(ChoiceField): 81 81 """ 82 82 A form field that validates its input is a U.S. state name or abbreviation. 83 83 It normalizes the input to the standard two-leter postal service … … 86 86 default_error_messages = { 87 87 'invalid': u'Enter a U.S. state or territory.', 88 88 } 89 89 90 def __init__(self, empty_label=u'---------', *args, **kwargs): 91 from us_states import STATE_CHOICES 92 ChoiceField.__init__(self, (('', empty_label),) + STATE_CHOICES, *args, **kwargs) 93 90 94 def clean(self, value): 91 95 from us_states import STATES_NORMALIZED 92 96 super(USStateField, self).clean(value) … … 102 106 except KeyError: 103 107 pass 104 108 raise ValidationError(self.error_messages['invalid']) 105 106 class USStateSelect(Select):107 """108 A Select widget that uses a list of U.S. states/territories as its choices.109 """110 def __init__(self, attrs=None):111 from us_states import STATE_CHOICES112 super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)