From 3f73df44f2726319f6af44c5613e5e531e9370b6 Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Tue, 11 Jul 2023 13:50:56 +0100 Subject: [PATCH] Fixed #34705 -- Reallowed BoundField.as_widget()'s attrs argument to set aria-describedby. Regression in 966ecdd482167f3f6b08b00f484936c837751cb9. --- django/forms/boundfield.py | 15 +++++++++------ tests/forms_tests/tests/test_forms.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index deba739329..0b4439efde 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -287,12 +287,15 @@ class BoundField(RenderableFieldMixin): attrs["required"] = True if self.field.disabled: attrs["disabled"] = True - # If a custom aria-describedby attribute is given and help_text is - # used, the custom aria-described by is preserved so user can set the - # desired order. - if custom_aria_described_by_id := widget.attrs.get("aria-describedby"): - attrs["aria-describedby"] = custom_aria_described_by_id - elif self.field.help_text and self.id_for_label: + # If a custom aria-describedby attribute is given (either via the attrs + # argument or widget.attrs) and help_text is used, the custom + # aria-described by is preserved so user can set the desired order. + if ( + not attrs.get("aria-describedby") + and not widget.attrs.get("aria-describedby") + and self.field.help_text + and self.id_for_label + ): attrs["aria-describedby"] = f"{self.id_for_label}_helptext" return attrs diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 99a38eedcb..0baddad69b 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3082,6 +3082,17 @@ Options: ', + ) + def test_subclassing_forms(self): # You can subclass a Form to add fields. The resulting form subclass will have # all of the fields of the parent Form, plus whichever fields you define in the