Fixed #29200 -- Fixed label rendering when using RadioSelect and CheckboxSelectMultiple with MultiWidget.

This commit is contained in:
Tim Graham 2018-03-14 11:28:18 -04:00
parent fb8fd535c0
commit 87dc0844a6
7 changed files with 32 additions and 5 deletions

View File

@ -248,7 +248,7 @@ class BoundWidget:
return self.tag(wrap_label=True)
def tag(self, wrap_label=False):
context = {'widget': self.data, 'wrap_label': wrap_label}
context = {'widget': {**self.data, 'wrap_label': wrap_label}}
return self.parent_widget._render(self.template_name, context, self.renderer)
@property

View File

@ -1 +1 @@
{% if wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if wrap_label %} {{ widget.label }}</label>{% endif %}
{% if widget.wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if widget.wrap_label %} {{ widget.label }}</label>{% endif %}

View File

@ -1 +1 @@
{% if wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if wrap_label %} {{ widget.label }}</label>{% endif %}
{% if widget.wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if widget.wrap_label %} {{ widget.label }}</label>{% endif %}

View File

@ -621,12 +621,12 @@ class ChoiceWidget(Widget):
'attrs': option_attrs,
'type': self.input_type,
'template_name': self.option_template_name,
'wrap_label': True,
}
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
context['wrap_label'] = True
return context
def id_for_label(self, id_, index='0'):

View File

@ -310,6 +310,15 @@ If you want to continue to allow those passwords to be used, you'll
have to define the :setting:`PASSWORD_HASHERS` setting (if you don't already)
and include ``'django.contrib.auth.hashers.BCryptPasswordHasher'``.
Moved ``wrap_label`` widget template context variable
-----------------------------------------------------
To fix the lack of ``<label>`` when using ``RadioSelect`` and
``CheckboxSelectMultiple`` with ``MultiWidget``, the ``wrap_label`` context
variable now appears as an attribute of each option. For example, in a custom
``input_option.html`` template, change ``{% if wrap_label %}`` to
``{% if widget.wrap_label %}``.
Miscellaneous
-------------

View File

@ -1,6 +1,6 @@
import datetime
from django.forms import RadioSelect
from django.forms import MultiWidget, RadioSelect
from django.test import override_settings
from .base import WidgetTest
@ -130,3 +130,16 @@ class RadioSelectTest(WidgetTest):
</ul>
"""
self.check_html(self.widget(choices=choices), 'time', None, html=html)
def test_render_as_subwidget(self):
"""A RadioSelect as a subwidget of MultiWidget."""
choices = (('', '------'),) + self.beatles
self.check_html(MultiWidget([self.widget(choices=choices)]), 'beatle', ['J'], html=(
"""<ul>
<li><label><input type="radio" name="beatle_0" value=""> ------</label></li>
<li><label><input checked type="radio" name="beatle_0" value="J"> John</label></li>
<li><label><input type="radio" name="beatle_0" value="P"> Paul</label></li>
<li><label><input type="radio" name="beatle_0" value="G"> George</label></li>
<li><label><input type="radio" name="beatle_0" value="R"> Ringo</label></li>
</ul>"""
))

View File

@ -294,6 +294,7 @@ class SelectTest(WidgetTest):
'template_name': 'django/forms/widgets/select_option.html',
'name': 'name',
'selected': False,
'wrap_label': True,
}, {
'value': 'cd',
'type': 'select',
@ -303,6 +304,7 @@ class SelectTest(WidgetTest):
'template_name': 'django/forms/widgets/select_option.html',
'name': 'name',
'selected': False,
'wrap_label': True,
}]
)
self.assertEqual(index, 0)
@ -319,6 +321,7 @@ class SelectTest(WidgetTest):
'name': 'name',
'selected': True,
'type': 'select',
'wrap_label': True,
}, {
'value': 'dvd',
'template_name': 'django/forms/widgets/select_option.html',
@ -328,6 +331,7 @@ class SelectTest(WidgetTest):
'name': 'name',
'selected': False,
'type': 'select',
'wrap_label': True,
}]
)
self.assertEqual(index, 1)
@ -344,6 +348,7 @@ class SelectTest(WidgetTest):
'index': '2',
'name': 'name',
'type': 'select',
'wrap_label': True,
}]
)
self.assertEqual(index, 2)