From: Mark Otto Date: Tue, 14 Apr 2026 22:59:17 +0000 (-0500) Subject: Add validation feedback IDs for a11y, set aria-invalid on controls on form submission... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=96414603eda7bdcd699abbaed3b541a8e1cac056;p=thirdparty%2Fbootstrap.git Add validation feedback IDs for a11y, set aria-invalid on controls on form submission in examples (#42313) --- diff --git a/site/src/content/docs/forms/validation.mdx b/site/src/content/docs/forms/validation.mdx index 12b169589f..72423fdcaa 100644 --- a/site/src/content/docs/forms/validation.mdx +++ b/site/src/content/docs/forms/validation.mdx @@ -31,49 +31,51 @@ For custom Bootstrap form validation messages, add `data-bs-validate` and the `n Use `data-bs-validate="valid"` to also show success styling on valid fields. Add `required` to each form input and `.invalid-feedback` to provide field-specific error messages. If you enable valid styling, use `.valid-feedback` for success messages. +**For accessibility,** add `aria-describedby` to each form control, pointing to the `id` of its feedback message. This ensures screen readers announce the error when the user focuses an invalid field. The example JavaScript below also sets `aria-invalid` on each control at submit time and clears it as users correct their input. + For example, try to submit the form below; our JavaScript will intercept the submit button and relay feedback to you.
- -
Full name is required
+ +
Full name is required
$ - +
-
Please enter a donation amount
+
Please enter a donation amount
- -
Please enter a mailing address
+ +
Please enter a mailing address
- -
Please provide a city
+ +
Please provide a city
- -
Please select a state
+
Please select a state
- -
Required
+ +
Required
@@ -189,9 +191,9 @@ For invalid fields, ensure that the invalid feedback/error message is associated
- -
Looks great!
-
Full name is required
+ +
Looks great!
+
Full name is required
@@ -206,9 +208,9 @@ For invalid fields, ensure that the invalid feedback/error message is associated
- -
Looks great!
-
Please enter a mailing address
+ +
Looks great!
+
Please enter a mailing address
@@ -279,8 +281,8 @@ Validation styles are available for the following form controls and components:
- -
+ +
Please enter a message in the textarea.
@@ -289,7 +291,7 @@ Validation styles are available for the following form controls and components:
- + @@ -297,7 +299,7 @@ Validation styles are available for the following form controls and components:
-
Example invalid feedback text
+
Example invalid feedback text
@@ -305,14 +307,14 @@ Validation styles are available for the following form controls and components:
- +
- +
-
More example invalid feedback text
+
More example invalid feedback text
@@ -321,44 +323,44 @@ Validation styles are available for the following form controls and components:
- +
-
Example invalid switch feedback
+
Example invalid switch feedback
- -
Example invalid select feedback
+
Example invalid select feedback
- -
Example invalid form file feedback
+ +
Example invalid form file feedback
- -
Example invalid range feedback
+ +
Example invalid range feedback
- +
-
Example invalid floating label feedback
+
Example invalid floating label feedback
@@ -367,27 +369,27 @@ Validation styles are available for the following form controls and components:
- +
-
Example invalid adorned input feedback
+
Example invalid adorned input feedback
@ - +
-
Please choose a username
+
Please choose a username
Example - +
-
Please add at least one tag
+
Please add at least one tag
`} /> @@ -403,43 +405,43 @@ This example functions the same as the [custom styles example](#custom-styles).
- -
Full name is required
+ +
Full name is required
$ - +
-
Please enter a donation amount
+
Please enter a donation amount
- -
Please enter a mailing address
+ +
Please enter a mailing address
- -
Please provide a city
+ +
Please provide a city
- -
Please select a state
+
Please select a state
- -
Required
+ +
Required
diff --git a/site/static/docs/[version]/assets/js/validate-forms.js b/site/static/docs/[version]/assets/js/validate-forms.js index 846733643f..7fc3adf673 100644 --- a/site/static/docs/[version]/assets/js/validate-forms.js +++ b/site/static/docs/[version]/assets/js/validate-forms.js @@ -10,6 +10,20 @@ event.preventDefault() event.stopPropagation() } + + for (const control of form.elements) { + if (control.willValidate) { + control.setAttribute('aria-invalid', String(!control.validity.valid)) + } + } + }) + + // Clear aria-invalid as users correct individual fields + form.addEventListener('input', event => { + const control = event.target + if (control.willValidate && control.hasAttribute('aria-invalid')) { + control.setAttribute('aria-invalid', String(!control.validity.valid)) + } }) } })()