Ticket #3139: prefix-errors.diff

File prefix-errors.diff, 2.3 KB (added by jkocherhans, 18 years ago)

The fix could probably be simpler, but I think this is more clear.

  • django/newforms/forms.py

     
    173173    def __init__(self, form, field, name):
    174174        self.form = form
    175175        self.field = field
    176         self.name = form.add_prefix(name)
     176        self.name = name
     177        self.html_name = form.add_prefix(name)
    177178        self.label = self.field.label or pretty_name(name)
    178179
    179180    def __unicode__(self):
     
    201202        auto_id = self.auto_id
    202203        if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'):
    203204            attrs['id'] = auto_id
    204         return widget.render(self.name, self.data, attrs=attrs)
     205        return widget.render(self.html_name, self.data, attrs=attrs)
    205206
    206207    def as_text(self, attrs=None):
    207208        """
     
    223224        "Returns the data for this BoundField, or None if it wasn't given."
    224225        if self.field.widget.requires_data_list and isinstance(self.form.data, MultiValueDict):
    225226            return self.form.data.getlist(self.name)
    226         return self.form.data.get(self.name, None)
     227        return self.form.data.get(self.html_name, None)
    227228    data = property(_data)
    228229
    229230    def label_tag(self, contents=None):
     
    251252        """
    252253        auto_id = self.form.auto_id
    253254        if auto_id and '%s' in str(auto_id):
    254             return str(auto_id) % self.name
     255            return str(auto_id) % self.html_name
    255256        elif auto_id:
    256257            return self.name
    257258        return ''
  • tests/regressiontests/forms/tests.py

     
    19751975>>> p.is_valid()
    19761976True
    19771977
     1978Let's try submitting some bad data to make sure form.errors and field.errors
     1979work as expected.
     1980>>> data = {
     1981...     'person1-first_name': u'',
     1982...     'person1-last_name': u'',
     1983...     'person1-birthday': u''
     1984... }
     1985>>> p = Person(data, prefix='person1')
     1986>>> len(p.errors)
     19873
     1988>>> print p['first_name'].errors.as_ul()
     1989<ul class="errorlist"><li>This field is required.</li></ul>
     1990
    19781991This is pretty unremarkable in and of itself, but let's create some data that
    19791992contains info for two different people.
    19801993>>> data = {
Back to Top