summaryrefslogtreecommitdiff
path: root/tests/forms_tests
diff options
context:
space:
mode:
authorGregor Jerše <gregor.jerse@genialis.com>2023-06-01 16:44:57 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-07-06 08:03:19 +0200
commit966ecdd482167f3f6b08b00f484936c837751cb9 (patch)
tree7876e7b05088ed60822604d8bd32d33e3d1f9d9d /tests/forms_tests
parent649262a406168709686f97694493aa1f717c6c96 (diff)
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.
Diffstat (limited to 'tests/forms_tests')
-rw-r--r--tests/forms_tests/tests/test_forms.py70
1 files changed, 68 insertions, 2 deletions
diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
index c283ab20ef..99a38eedcb 100644
--- a/tests/forms_tests/tests/test_forms.py
+++ b/tests/forms_tests/tests/test_forms.py
@@ -3016,6 +3016,72 @@ Options: <select multiple name="options" required>
"</td></tr>",
)
+ def test_widget_attrs_custom_aria_describedby(self):
+ # aria-describedby provided to the widget overrides the default.
+
+ class UserRegistration(Form):
+ username = CharField(
+ max_length=255,
+ help_text="e.g., user@example.com",
+ widget=TextInput(attrs={"aria-describedby": "custom-description"}),
+ )
+ password = CharField(
+ widget=PasswordInput, help_text="Wählen Sie mit Bedacht."
+ )
+
+ p = UserRegistration()
+ self.assertHTMLEqual(
+ p.as_div(),
+ '<div><label for="id_username">Username:</label>'
+ '<div class="helptext" id="id_username_helptext">e.g., user@example.com'
+ '</div><input type="text" name="username" maxlength="255" required '
+ 'aria-describedby="custom-description" id="id_username">'
+ "</div><div>"
+ '<label for="id_password">Password:</label>'
+ '<div class="helptext" id="id_password_helptext">Wählen Sie mit Bedacht.'
+ '</div><input type="password" name="password" required '
+ 'aria-describedby="id_password_helptext" id="id_password"></div>',
+ )
+ self.assertHTMLEqual(
+ p.as_ul(),
+ '<li><label for="id_username">Username:</label><input type="text" '
+ 'name="username" maxlength="255" required '
+ 'aria-describedby="custom-description" id="id_username">'
+ '<span class="helptext" id="id_username_helptext">e.g., user@example.com'
+ "</span></li><li>"
+ '<label for="id_password">Password:</label>'
+ '<input type="password" name="password" required '
+ 'aria-describedby="id_password_helptext" id="id_password">'
+ '<span class="helptext" id="id_password_helptext">Wählen Sie mit Bedacht.'
+ "</span></li>",
+ )
+ self.assertHTMLEqual(
+ p.as_p(),
+ '<p><label for="id_username">Username:</label><input type="text" '
+ 'name="username" maxlength="255" required '
+ 'aria-describedby="custom-description" id="id_username">'
+ '<span class="helptext" id="id_username_helptext">e.g., user@example.com'
+ "</span></p><p>"
+ '<label for="id_password">Password:</label>'
+ '<input type="password" name="password" required '
+ 'aria-describedby="id_password_helptext" id="id_password">'
+ '<span class="helptext" id="id_password_helptext">Wählen Sie mit Bedacht.'
+ "</span></p>",
+ )
+ self.assertHTMLEqual(
+ p.as_table(),
+ '<tr><th><label for="id_username">Username:</label></th><td>'
+ '<input type="text" name="username" maxlength="255" required '
+ 'aria-describedby="custom-description" id="id_username"><br>'
+ '<span class="helptext" id="id_username_helptext">e.g., user@example.com'
+ "</span></td></tr><tr><th>"
+ '<label for="id_password">Password:</label></th><td>'
+ '<input type="password" name="password" required '
+ 'aria-describedby="id_password_helptext" id="id_password"><br>'
+ '<span class="helptext" id="id_password_helptext">Wählen Sie mit Bedacht.'
+ "</span></td></tr>",
+ )
+
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
@@ -4796,7 +4862,7 @@ class TemplateTests(SimpleTestCase):
"<form>"
'<p><label for="id_username">Username:</label>'
'<input id="id_username" type="text" name="username" maxlength="10" '
- "required></p>"
+ 'aria-describedby="id_username_helptext" required></p>'
'<p><label for="id_password1">Password1:</label>'
'<input type="password" name="password1" id="id_password1" required></p>'
'<p><label for="id_password2">Password2:</label>'
@@ -4833,7 +4899,7 @@ class TemplateTests(SimpleTestCase):
"<form>"
'<p><legend for="id_username">Username:</legend>'
'<input id="id_username" type="text" name="username" maxlength="10" '
- "required></p>"
+ 'aria-describedby="id_username_helptext" required></p>'
'<p><legend for="id_password1">Password1:</legend>'
'<input type="password" name="password1" id="id_password1" required></p>'
'<p><legend for="id_password2">Password2:</legend>'