From 0eec2a163a4b2ea4e82c53ae1d7b71bf43ac911d Mon Sep 17 00:00:00 2001 From: Kasyap Pentamaraju Date: Tue, 11 Nov 2025 14:59:28 +0530 Subject: Fixed #36724 -- Removed invalid "for" attribute on tags. --- django/forms/boundfield.py | 4 +- tests/forms_tests/tests/test_forms.py | 60 ++++++++++++++-------- tests/forms_tests/tests/test_i18n.py | 4 +- .../widget_tests/test_clearablefileinput.py | 6 +-- .../widget_tests/test_selectdatewidget.py | 2 +- tests/model_forms/tests.py | 6 +-- tests/model_formsets/tests.py | 4 +- 7 files changed, 52 insertions(+), 34 deletions(-) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 97559fe286..5b8e7e5697 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -192,7 +192,9 @@ class BoundField(RenderableFieldMixin): if id_: id_for_label = widget.id_for_label(id_) if id_for_label: - attrs = {**(attrs or {}), "for": id_for_label} + attrs = attrs or {} + if tag != "legend": + attrs = {**attrs, "for": id_for_label} if self.field.required and hasattr(self.form, "required_css_class"): attrs = attrs or {} if "class" in attrs: diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 5b0c4b9a04..8cc1fecf0a 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3913,7 +3913,7 @@ aria-describedby="id_age_error">""", ) self.assertHTMLEqual( f["field"].legend_tag(), - 'Field:', + 'Field:', ) self.assertHTMLEqual( f["field"].label_tag(attrs={"class": "foo"}), @@ -3921,14 +3921,14 @@ aria-describedby="id_age_error">""", ) self.assertHTMLEqual( f["field"].legend_tag(attrs={"class": "foo"}), - 'Field:', + 'Field:', ) self.assertHTMLEqual( f["field2"].label_tag(), '' ) self.assertHTMLEqual( f["field2"].legend_tag(), - 'Field2:', + "Field2:", ) def test_label_split_datetime_not_displayed(self): @@ -4190,31 +4190,47 @@ aria-describedby="id_age_error">""", boundfield = SomeForm()["field"] - testcases = [ # (args, kwargs, expected) - # without anything: just print the