Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11843 - Give MultipleHiddenInput different IDs. Thanks Chris B…

…eaven.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12151 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 341c85bed0676e4bcd762186139367549ffceabb 1 parent 18723e6
Jannis Leidel authored January 09, 2010
13  django/forms/widgets.py
@@ -242,9 +242,16 @@ def __init__(self, attrs=None, choices=()):
242 242
     def render(self, name, value, attrs=None, choices=()):
243 243
         if value is None: value = []
244 244
         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
245  
-        return mark_safe(u'\n'.join([(u'<input%s />' %
246  
-            flatatt(dict(value=force_unicode(v), **final_attrs)))
247  
-            for v in value]))
  245
+        id_ = final_attrs.get('id', None)
  246
+        inputs = []
  247
+        for i, v in enumerate(value):
  248
+            input_attrs = dict(value=force_unicode(v), **final_attrs)
  249
+            if id_:
  250
+                # An ID attribute was given. Add a numeric index as a suffix
  251
+                # so that the inputs don't all have the same ID attribute.
  252
+                input_attrs['id'] = '%s_%s' % (id_, i)
  253
+            inputs.append(u'<input%s />' % flatatt(input_attrs))
  254
+        return mark_safe(u'\n'.join(inputs))
248 255
 
249 256
     def value_from_datadict(self, data, files, name):
250 257
         if isinstance(data, (MultiValueDict, MergeDict)):
13  tests/regressiontests/forms/widgets.py
@@ -183,6 +183,11 @@
183 183
 >>> w.render('email', ['foo@example.com'], attrs={'class': 'special'})
184 184
 u'<input type="hidden" class="special" value="foo@example.com" name="email" />'
185 185
 
  186
+Each input gets a separate ID.
  187
+>>> w = MultipleHiddenInput()
  188
+>>> w.render('letters', list('abc'), attrs={'id': 'hideme'})
  189
+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" />'
  190
+
186 191
 # FileInput Widget ############################################################
187 192
 
188 193
 FileInput widgets don't ever show the value, because the old value is of no use
@@ -1016,6 +1021,14 @@
1016 1021
 >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
1017 1022
 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>'
1018 1023
 
  1024
+# Each input gets a separate ID
  1025
+>>> print CheckboxSelectMultiple().render('letters', list('ac'), choices=zip(list('abc'), list('ABC')), attrs={'id': 'abc'})
  1026
+<ul>
  1027
+<li><label for="abc_0"><input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" /> A</label></li>
  1028
+<li><label for="abc_1"><input type="checkbox" name="letters" value="b" id="abc_1" /> B</label></li>
  1029
+<li><label for="abc_2"><input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" /> C</label></li>
  1030
+</ul>
  1031
+
1019 1032
 # MultiWidget #################################################################
1020 1033
 
1021 1034
 >>> class MyMultiWidget(MultiWidget):

0 notes on commit 341c85b

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