From 6af75bb7c76c71fa6e596738a2c26966f3b15f4c Mon Sep 17 00:00:00 2001 From: David Smith Date: Sun, 19 Nov 2023 19:26:12 +0000 Subject: [PATCH] Fixed #32819 -- Added aria-describedby to fields with errors. --- django/forms/boundfield.py | 2 + docs/ref/forms/api.txt | 55 +++++++--- docs/releases/5.2.txt | 4 + tests/forms_tests/tests/test_forms.py | 140 +++++++++++++++++++++----- 4 files changed, 159 insertions(+), 42 deletions(-) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 4d3cd88660..c0324d5c1d 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -305,6 +305,8 @@ class BoundField(RenderableFieldMixin): if self.auto_id and not self.is_hidden: if self.help_text: aria_describedby.append(f"{self.auto_id}_helptext") + if self.errors: + aria_describedby.append(f"{self.auto_id}_error") return " ".join(aria_describedby) @property diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index 494f3f244e..f068c31a93 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -986,14 +986,16 @@ the ``Form``, then the latter ``field_order`` will have precedence. You may rearrange the fields any time using ``order_fields()`` with a list of field names as in :attr:`~django.forms.Form.field_order`. +.. _form-error-display: + How errors are displayed ------------------------ If you render a bound ``Form`` object, the act of rendering will automatically run the form's validation if it hasn't already happened, and the HTML output -will include the validation errors as a ``