From c187f5f9242b681abaa199173e02066997439425 Mon Sep 17 00:00:00 2001 From: David Smith Date: Mon, 29 Apr 2024 08:13:35 +0100 Subject: [PATCH] Refs #32819 -- Avoided adding 'aria-describedby' to hidden inputs. Hidden elements are not visible for both accessibility tools and browsers presentation layer. This change therefore only reduces the size of the generated HTML. --- django/forms/boundfield.py | 1 + tests/forms_tests/tests/test_forms.py | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index d56fa0ea3e..d0d5c13b3d 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -298,6 +298,7 @@ class BoundField(RenderableFieldMixin): and self.field.help_text and not self.use_fieldset and self.auto_id + and not self.is_hidden ): attrs["aria-describedby"] = f"{self.auto_id}_helptext" return attrs diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index a86d443e33..3982cc93fe 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -2136,6 +2136,15 @@ class FormsTestCase(SimpleTestCase): p.as_p(), '' ) + def test_hidden_widget_does_not_have_aria_describedby(self): + class TestForm(Form): + hidden_text = CharField(widget=HiddenInput, help_text="Help Text") + + f = TestForm() + self.assertEqual( + str(f), '' + ) + def test_field_order(self): # A Form's fields are displayed in the same order in which they were defined. class TestForm(Form):