docs(forms): explain validation feedback display Show how a control's state selects its feedback message. Explain which validation settings apply and how to associate the feedback with the control.
diff --git a/site/src/content/docs/forms/validation.mdx b/site/src/content/docs/forms/validation.mdx index 4785e03..6abf09a 100644 --- a/site/src/content/docs/forms/validation.mdx +++ b/site/src/content/docs/forms/validation.mdx
@@ -23,7 +23,14 @@ - Feedback messages may utilize the [browser defaults](#browser-defaults) (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS. - You may provide custom validity messages with `setCustomValidity` in JavaScript. -With that in mind, consider the following demos for our custom form validation styles, optional server-side classes, and browser defaults. +## Feedback display + +Place `.valid-feedback` and `.invalid-feedback` after the control as siblings. CSS hides both by default and shows the message that matches the control's state. Some components use a wrapper for this relationship, as shown in the examples below. + +- For client-side validation, `data-bs-validate` enables feedback for `:user-invalid` controls. Use `data-bs-validate="valid"` to also show feedback for `:user-valid` controls. +- For server-side validation, put either `.is-valid` or `.is-invalid` on the control. These classes work without `data-bs-validate`. + +Use `aria-describedby` to associate the applicable message with the control. Referencing a message does not change its visual display. When you render a server-side response, reference only the feedback for the current state. ## Custom styles @@ -183,7 +190,7 @@ ## Server-side -We recommend using client-side validation, but in case you require server-side validation, you can indicate invalid and valid form fields with `.is-invalid` and `.is-valid`. Note that `.invalid-feedback` is also supported with these classes. +We recommend using client-side validation, but in case you require server-side validation, you can indicate invalid and valid form fields with `.is-invalid` and `.is-valid`. Use `.invalid-feedback` with `.is-invalid` and `.valid-feedback` with `.is-valid`. For invalid fields, ensure that the invalid feedback/error message is associated with the relevant form field using `aria-describedby` (noting that this attribute allows more than one `id` to be referenced, in case the field already points to additional form text). @@ -191,7 +198,7 @@ <div class="grid gap-5"> <div class="form-field md:g-col-6"> <label for="validationServer01" class="form-label">Name</label> - <input type="text" class="form-control is-valid" id="validationServer01" value="Mark Otto" aria-describedby="validationServer01Valid validationServer01Invalid"><!-- [!code highlight] --> + <input type="text" class="form-control is-valid" id="validationServer01" value="Mark Otto" aria-describedby="validationServer01Valid"><!-- [!code highlight] --> <div id="validationServer01Valid" class="valid-feedback">Looks great!</div><!-- [!code highlight] --> <div id="validationServer01Invalid" class="invalid-feedback">Full name is required</div><!-- [!code highlight] --> </div> @@ -199,7 +206,7 @@ <label for="validationServerDonation" class="form-label">Donation amount</label> <div class="input-group"> <span class="input-group-text" id="inputGroupPrepend3">$</span> - <input type="number" class="form-control is-invalid" value="" id="validationServerDonation" aria-describedby="inputGroupPrepend3 serverDonateFeedbackValid serverDonateFeedbackInvalid"><!-- [!code highlight] --> + <input type="number" class="form-control is-invalid" value="" id="validationServerDonation" aria-describedby="inputGroupPrepend3 serverDonateFeedbackInvalid"><!-- [!code highlight] --> </div> <div id="serverDonateFeedbackValid" class="valid-feedback">Looks great!</div><!-- [!code highlight] --> <div id="serverDonateFeedbackInvalid" class="invalid-feedback">Please enter a donation amount</div><!-- [!code highlight] --> @@ -208,7 +215,7 @@ <div class="form-field"> <label for="validationServerAddress" class="form-label">Address</label> - <input type="text" class="form-control is-valid" id="validationServerAddress" value="1234 Main St" aria-describedby="validationServerAddressValid validationServerAddressInvalid"><!-- [!code highlight] --> + <input type="text" class="form-control is-valid" id="validationServerAddress" value="1234 Main St" aria-describedby="validationServerAddressValid"><!-- [!code highlight] --> <div id="validationServerAddressValid" class="valid-feedback">Looks great!</div><!-- [!code highlight] --> <div id="validationServerAddressInvalid" class="invalid-feedback">Please enter a mailing address</div> </div>