Fixed #27250 -- Removed 'for ="..."' from CheckboxSelectMultiple's <label>.

This commit is contained in:
Diego Martín 2016-10-28 19:21:45 +02:00 committed by Tim Graham
parent f62abfc03d
commit abd434059e
2 changed files with 20 additions and 0 deletions

View File

@ -822,6 +822,13 @@ class CheckboxSelectMultiple(RendererMixin, SelectMultiple):
# never known if the value is actually omitted. # never known if the value is actually omitted.
return False return False
def id_for_label(self, id_):
""""
Don't include for="field_0" in <label> because clicking such a label
would toggle the first checkbox.
"""
return ''
class MultiWidget(Widget): class MultiWidget(Widget):
""" """

View File

@ -1,3 +1,4 @@
from django import forms
from django.forms import CheckboxSelectMultiple from django.forms import CheckboxSelectMultiple
from .base import WidgetTest from .base import WidgetTest
@ -125,3 +126,15 @@ class CheckboxSelectMultipleTest(WidgetTest):
widget = self.widget(choices=self.beatles) widget = self.widget(choices=self.beatles)
self.assertIs(widget.value_omitted_from_data({}, {}, 'field'), False) self.assertIs(widget.value_omitted_from_data({}, {}, 'field'), False)
self.assertIs(widget.value_omitted_from_data({'field': 'value'}, {}, 'field'), False) self.assertIs(widget.value_omitted_from_data({'field': 'value'}, {}, 'field'), False)
def test_label(self):
""""
CheckboxSelectMultiple doesn't contain 'for="field_0"' in the <label>
because clicking that would toggle the first checkbox.
"""
class TestForm(forms.Form):
f = forms.MultipleChoiceField(widget=CheckboxSelectMultiple)
bound_field = TestForm()['f']
self.assertEqual(bound_field.field.widget.id_for_label('id'), '')
self.assertEqual(bound_field.label_tag(), '<label>F:</label>')