Fixed #33235 -- Removed "for = ..." from MultiWidget's <label>.

This improves accessibility for screen reader users.
This commit is contained in:
David Smith 2021-11-05 08:11:25 +00:00 committed by Mariusz Felisiak
parent 2c01ebb4be
commit c6c6cd3c5a
4 changed files with 21 additions and 10 deletions

View File

@ -849,9 +849,7 @@ class MultiWidget(Widget):
return context return context
def id_for_label(self, id_): def id_for_label(self, id_):
if id_: return ''
id_ += '_0'
return id_
def value_from_datadict(self, data, files, name): def value_from_datadict(self, data, files, name):
return [ return [

View File

@ -141,7 +141,7 @@ class MultiValueFieldTest(SimpleTestCase):
self.assertHTMLEqual( self.assertHTMLEqual(
form.as_table(), form.as_table(),
""" """
<tr><th><label for="id_field1_0">Field1:</label></th> <tr><th><label>Field1:</label></th>
<td><input type="text" name="field1_0" id="id_field1_0" required> <td><input type="text" name="field1_0" id="id_field1_0" required>
<select multiple name="field1_1" id="id_field1_1" required> <select multiple name="field1_1" id="id_field1_1" required>
<option value="J">John</option> <option value="J">John</option>
@ -164,7 +164,7 @@ class MultiValueFieldTest(SimpleTestCase):
self.assertHTMLEqual( self.assertHTMLEqual(
form.as_table(), form.as_table(),
""" """
<tr><th><label for="id_field1_0">Field1:</label></th> <tr><th><label>Field1:</label></th>
<td><input type="text" name="field1_0" value="some text" id="id_field1_0" required> <td><input type="text" name="field1_0" value="some text" id="id_field1_0" required>
<select multiple name="field1_1" id="id_field1_1" required> <select multiple name="field1_1" id="id_field1_1" required>
<option value="J" selected>John</option> <option value="J" selected>John</option>

View File

@ -1,7 +1,7 @@
import datetime import datetime
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.forms import SplitDateTimeField from django.forms import Form, SplitDateTimeField
from django.forms.widgets import SplitDateTimeWidget from django.forms.widgets import SplitDateTimeWidget
from django.test import SimpleTestCase from django.test import SimpleTestCase
@ -60,3 +60,16 @@ class SplitDateTimeFieldTest(SimpleTestCase):
self.assertTrue(f.has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['2008-05-06', '12:40:00'])) self.assertTrue(f.has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['2008-05-06', '12:40:00']))
self.assertFalse(f.has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['06/05/2008', '12:40'])) self.assertFalse(f.has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['06/05/2008', '12:40']))
self.assertTrue(f.has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['06/05/2008', '12:41'])) self.assertTrue(f.has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['06/05/2008', '12:41']))
def test_form_as_table(self):
class TestForm(Form):
datetime = SplitDateTimeField()
f = TestForm()
self.assertHTMLEqual(
f.as_table(),
'<tr><th><label>Datetime:</label></th><td>'
'<input type="text" name="datetime_0" required id="id_datetime_0">'
'<input type="text" name="datetime_1" required id="id_datetime_1">'
'</td></tr>',
)

View File

@ -665,7 +665,7 @@ class TestFormField(PostgreSQLSimpleTestCase):
self.assertHTMLEqual(str(form), ''' self.assertHTMLEqual(str(form), '''
<tr> <tr>
<th> <th>
<label for="id_field_0">Field:</label> <label>Field:</label>
</th> </th>
<td> <td>
<input id="id_field_0_0" name="field_0_0" type="text"> <input id="id_field_0_0" name="field_0_0" type="text">
@ -700,7 +700,7 @@ class TestFormField(PostgreSQLSimpleTestCase):
form.as_table(), form.as_table(),
""" """
<tr><th> <tr><th>
<label for="id_datetime_field_0">Datetime field:</label> <label>Datetime field:</label>
</th><td> </th><td>
<input type="text" name="datetime_field_0" id="id_datetime_field_0"> <input type="text" name="datetime_field_0" id="id_datetime_field_0">
<input type="text" name="datetime_field_1" id="id_datetime_field_1"> <input type="text" name="datetime_field_1" id="id_datetime_field_1">
@ -717,7 +717,7 @@ class TestFormField(PostgreSQLSimpleTestCase):
form.as_table(), form.as_table(),
""" """
<tr><th> <tr><th>
<label for="id_datetime_field_0">Datetime field:</label> <label>Datetime field:</label>
</th><td> </th><td>
<input type="text" name="datetime_field_0" <input type="text" name="datetime_field_0"
value="2010-01-01 11:13:00" id="id_datetime_field_0"> value="2010-01-01 11:13:00" id="id_datetime_field_0">
@ -754,7 +754,7 @@ class TestFormField(PostgreSQLSimpleTestCase):
self.assertHTMLEqual(str(RangeForm()), ''' self.assertHTMLEqual(str(RangeForm()), '''
<tr> <tr>
<th><label for="id_ints_0">Ints:</label></th> <th><label>Ints:</label></th>
<td> <td>
<input id="id_ints_0" name="ints_0" type="number"> <input id="id_ints_0" name="ints_0" type="number">
<input id="id_ints_1" name="ints_1" type="number"> <input id="id_ints_1" name="ints_1" type="number">