Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.1.X] Fixed #11843 - Give MultipleHiddenInput different IDs. Thanks…

… Chris Beaven.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 889c110e51946deb27fb41857cea0f2bbaa6a6a0 1 parent 8eaffea
@jezdez jezdez authored
Showing with 23 additions and 3 deletions.
  1. +10 −3 django/forms/widgets.py
  2. +13 −0 tests/regressiontests/forms/widgets.py
View
13 django/forms/widgets.py
@@ -242,9 +242,16 @@ def __init__(self, attrs=None, choices=()):
def render(self, name, value, attrs=None, choices=()):
if value is None: value = []
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
- return mark_safe(u'\n'.join([(u'<input%s />' %
- flatatt(dict(value=force_unicode(v), **final_attrs)))
- for v in value]))
+ id_ = final_attrs.get('id', None)
+ inputs = []
+ for i, v in enumerate(value):
+ input_attrs = dict(value=force_unicode(v), **final_attrs)
+ if id_:
+ # An ID attribute was given. Add a numeric index as a suffix
+ # so that the inputs don't all have the same ID attribute.
+ input_attrs['id'] = '%s_%s' % (id_, i)
+ inputs.append(u'<input%s />' % flatatt(input_attrs))
+ return mark_safe(u'\n'.join(inputs))
def value_from_datadict(self, data, files, name):
if isinstance(data, (MultiValueDict, MergeDict)):
View
13 tests/regressiontests/forms/widgets.py
@@ -181,6 +181,11 @@
>>> w.render('email', ['foo@example.com'], attrs={'class': 'special'})
u'<input type="hidden" class="special" value="foo@example.com" name="email" />'
+Each input gets a separate ID.
+>>> w = MultipleHiddenInput()
+>>> w.render('letters', list('abc'), attrs={'id': 'hideme'})
+u'<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" />'
+
# FileInput Widget ############################################################
FileInput widgets don't ever show the value, because the old value is of no use
@@ -1014,6 +1019,14 @@
>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
u'<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>'
+# Each input gets a separate ID
+>>> print CheckboxSelectMultiple().render('letters', list('ac'), choices=zip(list('abc'), list('ABC')), attrs={'id': 'abc'})
+<ul>
+<li><label for="abc_0"><input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" /> A</label></li>
+<li><label for="abc_1"><input type="checkbox" name="letters" value="b" id="abc_1" /> B</label></li>
+<li><label for="abc_2"><input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" /> C</label></li>
+</ul>
+
# MultiWidget #################################################################
>>> class MyMultiWidget(MultiWidget):

0 comments on commit 889c110

Please sign in to comment.
Something went wrong with that request. Please try again.