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 cf6da63..a4f34df 100644 --- a/site/src/content/docs/forms/validation.mdx +++ b/site/src/content/docs/forms/validation.mdx
@@ -26,7 +26,26 @@ - 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 + +Put `.valid-feedback` and `.invalid-feedback` after the form control as siblings. Both messages start hidden. CSS shows the message that matches the control's state. + +- For client-side validation, add `.was-validated` to the form when you want to show feedback. A valid control shows `.valid-feedback`. An invalid control shows `.invalid-feedback`. +- For server-side validation, set either `.is-valid` or `.is-invalid` on the control when you render the response. These classes work without `.was-validated`. + +The visual example below starts with `.was-validated` already applied. Enter a valid email address to see the success message. + +<Example code={`<div class="was-validated"> + <label for="validationFeedbackEmail" class="form-label">Email address</label> + <input type="email" class="form-control" id="validationFeedbackEmail" required aria-describedby="validationFeedbackHint"> + <div id="validationFeedbackHint" class="form-text">Use a format like name@example.com.</div> + <div class="valid-feedback">The email format is valid.</div> + <div class="invalid-feedback">Enter a valid email address.</div> + </div>`} /> + +`aria-describedby` associates a description with the control. It does not change which message CSS shows. For server-side errors, reference the error message that applies to the field. + +Use `setCustomValidity(message)` to set a browser validation error from JavaScript. It changes the control's validity and blocking behavior. Clear it with `setCustomValidity('')` when the error is resolved. Server-rendered state classes show feedback with no JavaScript. ## Custom styles @@ -153,7 +172,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).