LibWeb: Implement HTMLFormElement::checkValidity (constraint validation)

This change implements the requirements from the HTML spec at
https://html.spec.whatwg.org/#statically-validate-the-constraints
and https://html.spec.whatwg.org/#dom-form-checkvalidity — the parts of
the HTML constraint validation API (aka “client-side form validation”)
https://html.spec.whatwg.org/#the-constraint-validation-api for
HTMLFormElement itself — as well as the code for the requirements at
https://html.spec.whatwg.org/#check-validity-steps, which are the shared
requirements for the checkValidity method for individual form controls.
This commit is contained in:
sideshowbarker 2025-02-22 22:14:44 +09:00 committed by Tim Ledbetter
commit 7c34746571
Notes: github-actions[bot] 2025-02-26 04:14:35 +00:00
4 changed files with 62 additions and 2 deletions

View file

@ -226,6 +226,20 @@ WebIDL::ExceptionOr<void> FormAssociatedElement::set_form_action(String const& v
return html_element.set_attribute(HTML::AttributeNames::formaction, value);
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#check-validity-steps
bool FormAssociatedElement::check_validity_steps()
{
// 1. If element is a candidate for constraint validation and does not satisfy its constraints
if (is_candidate_for_constraint_validation() && !satisfies_its_constraints()) {
auto& element = form_associated_element_to_html_element();
// 1. Fire an event named invalid at element, with the cancelable attribute initialized to true
element.dispatch_event(DOM::Event::create(element.realm(), EventNames::invalid, { .cancelable = true }));
// 2. Return false.
return false;
}
return true;
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#candidate-for-constraint-validation
bool FormAssociatedElement::is_candidate_for_constraint_validation() const
{