Ticket #11843: 11843.2.diff

File 11843.2.diff, 3.3 KB (added by Chris Beaven, 15 years ago)

with tests

  • django/forms/widgets.py

     
    247247    def render(self, name, value, attrs=None, choices=()):
    248248        if value is None: value = []
    249249        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
    250         return mark_safe(u'\n'.join([(u'<input%s />' %
    251             flatatt(dict(value=force_unicode(v), **final_attrs)))
    252             for v in value]))
     250        id_attr = final_attrs.get('id')
     251        inputs = []
     252        for i, v in enumerate(value):
     253            input_attrs = dict(value=force_unicode(v), **final_attrs)
     254            if id_attr:
     255                # An ID attribute was given. Add a numeric index as a suffix
     256                # so that the inputs don't all have the same ID attribute.
     257                input_attrs['id'] = '%s_%s' % (id_attr, i)
     258            inputs.append(u'<input%s />' % flatatt(input_attrs))
     259        return mark_safe(u'\n'.join(inputs))
    253260
    254261    def value_from_datadict(self, data, files, name):
    255262        if isinstance(data, (MultiValueDict, MergeDict)):
  • tests/regressiontests/forms/widgets.py

     
    181181>>> w.render('email', ['foo@example.com'], attrs={'class': 'special'})
    182182u'<input type="hidden" class="special" value="foo@example.com" name="email" />'
    183183
     184Each input gets a separate ID.
     185>>> w = MultipleHiddenInput()
     186>>> w.render('letters', list('abc'), attrs={'id': 'hideme'})
     187u'<input type="hidden" name="letters" value="a" id="hideme_0" />\n<input type="hidden" name="letters" value="b" id="hideme_1" />\n<input type="hidden" name="letters" value="c" id="hideme_2" />'
     188
    184189# FileInput Widget ############################################################
    185190
    186191FileInput widgets don't ever show the value, because the old value is of no use
     
    10141019>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
    10151020u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums" value="2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
    10161021
     1022# Each input gets a separate ID
     1023>>> print CheckboxSelectMultiple().render('letters', list('ac'), choices=zip(list('abc'), list('ABC')), attrs={'id': 'abc'})
     1024<ul>
     1025<li><label for="abc_0"><input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" /> A</label></li>
     1026<li><label for="abc_1"><input type="checkbox" name="letters" value="b" id="abc_1" /> B</label></li>
     1027<li><label for="abc_2"><input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" /> C</label></li>
     1028</ul>
     1029
    10171030# MultiWidget #################################################################
    10181031
    10191032>>> class MyMultiWidget(MultiWidget):
Back to Top