Fixed #19581 -- ensure unique html ids with CheckboxSelectMultiple widgets
ID check is now done the same way as MultipleHiddenInput.
This commit is contained in:
parent
227bd3f8db
commit
62f842e2e5
|
@ -754,17 +754,17 @@ class RadioSelect(Select):
|
||||||
class CheckboxSelectMultiple(SelectMultiple):
|
class CheckboxSelectMultiple(SelectMultiple):
|
||||||
def render(self, name, value, attrs=None, choices=()):
|
def render(self, name, value, attrs=None, choices=()):
|
||||||
if value is None: value = []
|
if value is None: value = []
|
||||||
has_id = attrs and 'id' in attrs
|
|
||||||
final_attrs = self.build_attrs(attrs, name=name)
|
final_attrs = self.build_attrs(attrs, name=name)
|
||||||
|
id_ = final_attrs.get('id', None)
|
||||||
output = ['<ul>']
|
output = ['<ul>']
|
||||||
# Normalize to strings
|
# Normalize to strings
|
||||||
str_values = set([force_text(v) for v in value])
|
str_values = set([force_text(v) for v in value])
|
||||||
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
|
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
|
||||||
# If an ID attribute was given, add a numeric index as a suffix,
|
# If an ID attribute was given, add a numeric index as a suffix,
|
||||||
# so that the checkboxes don't all have the same ID attribute.
|
# so that the checkboxes don't all have the same ID attribute.
|
||||||
if has_id:
|
if id_:
|
||||||
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
|
final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
|
||||||
label_for = format_html(' for="{0}"', final_attrs['id'])
|
label_for = format_html(' for="{0}_{1}"', id_, i)
|
||||||
else:
|
else:
|
||||||
label_for = ''
|
label_for = ''
|
||||||
|
|
||||||
|
|
|
@ -861,6 +861,13 @@ beatle J R Ringo False""")
|
||||||
<li><label for="abc_0"><input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" /> A</label></li>
|
<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_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>
|
<li><label for="abc_2"><input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" /> C</label></li>
|
||||||
|
</ul>""")
|
||||||
|
|
||||||
|
# Each input gets a separate ID when the ID is passed to the constructor
|
||||||
|
self.assertHTMLEqual(CheckboxSelectMultiple(attrs={'id': 'abc'}).render('letters', list('ac'), choices=zip(list('abc'), list('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>""")
|
</ul>""")
|
||||||
|
|
||||||
def test_multi(self):
|
def test_multi(self):
|
||||||
|
|
Loading…
Reference in New Issue