Ticket #6564: usstate.diff

File usstate.diff, 2.4 KB (added by Purdea Andrei, 17 years ago)
  • django/db/models/fields/__init__.py

     
    10551055        return [oldforms.USStateField]
    10561056
    10571057    def formfield(self, **kwargs):
    1058         from django.contrib.localflavor.us.forms import USStateSelect
    1059         defaults = {'widget': USStateSelect}
     1058        from django.contrib.localflavor.us.forms import USStateField as USStateFormField
     1059        defaults = {'form_class': USStateFormField}
    10601060        defaults.update(kwargs)
    10611061        return super(USStateField, self).formfield(**defaults)
    10621062
  • django/contrib/localflavor/us/forms.py

     
    33"""
    44
    55from django.newforms import ValidationError
    6 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
     6from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES, ChoiceField
    77from django.utils.encoding import smart_unicode
    88from django.utils.translation import ugettext
    99import re
     
    7777            raise ValidationError(self.error_messages['invalid'])
    7878        return u'%s-%s-%s' % (area, group, serial)
    7979
    80 class USStateField(Field):
     80class USStateField(ChoiceField):
    8181    """
    8282    A form field that validates its input is a U.S. state name or abbreviation.
    8383    It normalizes the input to the standard two-leter postal service
     
    8686    default_error_messages = {
    8787        'invalid': u'Enter a U.S. state or territory.',
    8888    }
    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   
    9094    def clean(self, value):
    9195        from us_states import STATES_NORMALIZED
    9296        super(USStateField, self).clean(value)
     
    102106            except KeyError:
    103107                pass
    104108        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_CHOICES
    112         super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
Back to Top