Bootstrap 5 validation: combining live per-field feedback with the .was-validated submit-gated pattern
0 reputation · 22 Nov 2022, 18:48 UTC
0 reputation · 22 Nov 2022, 18:48 UTC
I want a Bootstrap 5 form where invalid fields show error styling immediately as the user leaves or edits them, instead of only after the first submit attempt, while still following the documented .was-validated pattern as closely as possible.
The documented approach adds .was-validated to the form on submit, so :invalid styles only appear after that point. Bootstrap's validation classes are purely presentational and the framework ships no validation component, so any live feedback has to be wired up manually with the native constraint validation API. What is unclear is the intended way to layer per-field, per-event styling on top of this without fighting the CSS: adding .is-invalid directly works for server-side errors, but mixing it with .was-validated on the same form seems to create two sources of truth for which fields look invalid. The .invalid-feedback sibling selector requirement also means dynamically toggling classes must keep the markup structure intact.
.is-invalid with .was-validated, which takes visual precedence and can they conflict?33275 reputation · 23 Nov 2022, 04:09 UTC
.was‑validated, and to wire native Constraint Validation API listeners (e.g., input and blur) that toggle .is‑invalid on each control as soon as the user interacts with it. This keeps untouched fields neutral until the form is submitted, while still showing live feedback on the fields that have been touched..is‑invalid with .was‑validated, which takes visual precedence and can they conflict? The selector for .is‑invalid is a class selector applied directly to the control, so it has higher specificity than the .was‑validated .form-control:invalid rule. Consequently, once .is‑invalid is present, its styles override the pseudo‑class styles. The two mechanisms therefore coexist without visual conflict, provided you avoid duplicating error messages in the markup..form-control followed by an adjacent .invalid-feedback element, and the form receiving .was‑validated on submit—is identical in both versions. Bootstrap 5 removed jQuery and tweaked some internal selectors, but the public API for validation styles remains unchanged, so the same strategy applies to both..was‑validated when form.checkValidity() fails.required, type="email"), add a blur listener that marks the element as “touched” (for example, el.dataset.touched = true). Add an input listener that, if the element is touched, toggles .is‑invalid based on el.checkValidity()..is‑invalid overrides .was‑validated, you can safely remove the class when the field becomes valid again.setCustomValidity() for custom rules, remember to clear the message when the value becomes valid; otherwise the native :invalid state will stay true and both mechanisms will keep showing the error.If your form uses custom JavaScript‑only validation (no native constraints) or relies heavily on setCustomValidity(), the exact timing of when you apply or clear .is‑invalid may need adjustment. Let us know if that’s the case so the guidance can be refined.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.