From 966ecdd482167f3f6b08b00f484936c837751cb9 Mon Sep 17 00:00:00 2001 From: Gregor Jerše Date: Thu, 1 Jun 2023 16:44:57 +0200 Subject: Fixed #32819 -- Established relationship between form fields and their help text. Thanks Nimra for the initial patch. Thanks Natalia Bidart, Thibaud Colas, David Smith, and Mariusz Felisiak for reviews. --- docs/ref/forms/fields.txt | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'docs/ref/forms') diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index e443e9eabf..77ccc9e862 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -275,6 +275,48 @@ fields. We've specified ``auto_id=False`` to simplify the output:
Sender:
A valid email address, please.
Cc myself:
+When a field has help text and :attr:`~django.forms.BoundField.id_for_label` +returns a value, we associate ``help_text`` with the input using the +``aria-describedby`` HTML attribute: + +.. code-block:: pycon + + >>> from django import forms + >>> class UserForm(forms.Form): + ... username = forms.CharField(max_length=255, help_text="e.g., user@example.com") + ... + >>> f = UserForm() + >>> print(f) +
+ +
e.g., user@example.com
+ +
+ +When adding a custom ``aria-describedby`` attribute, make sure to also include +the ``id`` of the ``help_text`` element (if used) in the desired order. For +screen reader users, descriptions will be read in their order of appearance +inside ``aria-describedby``: + +.. code-block:: pycon + + >>> class UserForm(forms.Form): + ... username = forms.CharField( + ... max_length=255, + ... help_text="e.g., user@example.com", + ... widget=forms.TextInput( + ... attrs={"aria-describedby": "custom-description id_username_helptext"}, + ... ), + ... ) + ... + >>> f = UserForm() + >>> print(f["username"]) + + +.. versionchanged:: 5.0 + + ``aria-describedby`` was added to associate ``help_text`` with its input. + ``error_messages`` ------------------ -- cgit v1.3