Introduce the checkbox
optional validator
#30
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While
<input type="checkbox">
elements do support built-in Constraint Validations like ValidityState.valueMissing, most of theValidityState
properties will always befalse
. The Constraint Validations API determines the form control'sValidityState.valueMissing
property from its required attribute.When a form requires that a single
<input type="checkbox">
choice (like an acknowledgement of terms) is checked, the built-in support works well enough. When a form requires that at least one checkbox in a group of checkboxes ischecked
, the built-in support can be more strict than expected. For example, if there were multiple<input type="checkbox">
elements with the same[name]
attribute, and each element had the[required]
attribute, they would all need to be checked to be considered valid.ConstraintValidations
-powered validations support an experimentalcheckbox:
validator option to validate<input type="checkbox">
elements that share the same[name]
attribute as a group. To opt-into support, configure theConstraintValidations
instance:Then, render a group of
<input type="checkbox">
elements as[required]
:How it works
To work-around the quirks of built-in support,
ConstraintValidations
monitors when<input type="checkbox" required>
elements are connected to the document.Once connected,
ConstraintValidations
removes their[required]
attribute, then replaces it with an[aria-required="true"]
attribute instead. During form control validation, it utilizes the[aria-required="true"]
attributes to determine whether or not the collective group meets theValidityState.valueMissing
criteria.This technique integrates with other built-in mechanisms like:
[aria-invalid="true"]
CSS selectorHowever, its deviates from other built-in mechanism. For example: