-
-
Notifications
You must be signed in to change notification settings - Fork 79k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only validate required form controls #24904
Conversation
Does restricting it to only |
hmm, good point @tmorehouse ... worth testing |
This is not quite true... one would want to ensure an optional input does meet any constraints placed on it ( Imagine a form with two optional fields such as email address and website, which should contain valid formatted entries if values are supplied. the proposed solution above will not style these two form-controls if invalid values are supplied (one might only get the native browser tooltip error). Here is a mockup that lets one validate optional form fields with constraints. If the form field value is empty, no validation styling applies. If they do have value it is checked for invalidity: https://codepen.io/tmorehouse/pen/QOZZYd The following CSS is used (based on https://css-tricks.com/almanac/selectors/i/invalid/#points-of-interest): input:optional:invalid:not([value=""]),
input:required:invalid {
background: hsla(0, 90%, 70%, 1);
}
input:required:valid {
background: hsla(100, 90%, 70%, 1);
} This appears to work well for textual style inputs and selects, and will style invalid non-empty constrained optional inputs. I didn't test with Remove the selector I was unable to get a "valid" optional non empty input to show green though (tested in Firefox using selector You may need to target |
Testing this out, the problem with the |
Trying to play with this more: https://codepen.io/anon/pen/MOLEzK?editors=1100. Downside still is optional inputs that are valid with a non-zero value don't show green, but do when the input is invalid. Note the email address here: |
Yeah, I was unable to make valid optional inputs green, but they do show red when invalid, and assuming the input has not preset initial value. |
Arguably optional fields, even with constraints met, would be ok to show unstyled I'd say. Tricky conundrum... |
And optional fields that do not meet constraints should be styled invalid. For me, styled |
May be valid inputs shouldn't be styled at all? or make the valid styling opt-in, and only style invalid inputs (as most cases one would want to point the user to fields that are wrong, rather than ones that are right)? |
Going to wait on this and see what folks think after v4 goes stable. |
Maybe it’s worth considering some usability studies, for example this one https://baymard.com/blog/inline-form-validation |
Closing as stale. |
This changes the form validation styles to only apply to elements with an explicit
required
attribute. While working on a new form example for #24898, it caught me off guard that we were saying optional inputs werevalid
when in fact there's nothing to actually validate. This change ensures that only required inputs, selects, textareas, and custom form controls are validated and effectively treats all non-required
form controls as opt-in for form validation styles.Here's the before and after to show you what I mean. Note how the second address line and two checkboxes shouldn't be green as it implies we performed validation on
value
s that don't exist.