Skip to content
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

Implement HTMLFormElement::checkValidity and all HTMLInputElement “suffering from being missing” constraints (client-side form validation) #3674

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sideshowbarker
Copy link
Contributor

@sideshowbarker sideshowbarker commented Feb 23, 2025

This PR implements some core parts of the HTML “constraint-validation API” (aka “client-side form validation”):

  • for the form element itself: the HTMLFormElement::checkValidity method
  • the code for the shared requirements for the checkValidity method for individual form controls

The PR also implements all “suffering from being missing” constraints for HTMLInputElement.

This change gets us a number of new passes for some of the 130 subtests of the test at https://wpt.fyi/results/html/semantics/forms/constraints/form-validation-checkValidity.html?product=ladybird. We should be able to get to passing 100% of those subtests in subsequent PRs that fill out the remaining constraint checks from that spec (and that are already stubbed out in the code, from a previous PR).

@sideshowbarker sideshowbarker changed the title Implement HTMLFormElement::checkValidity and HTMLInputElement::suffering_from_being_missing (client-side form validation) Implement HTMLFormElement::checkValidity and all HTMLInputElement “suffering from being missing” constraints (client-side form validation) Feb 23, 2025
@sideshowbarker sideshowbarker force-pushed the client-side-form-validation branch from 97ee7be to fbb15a0 Compare February 23, 2025 11:53
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.
@sideshowbarker sideshowbarker force-pushed the client-side-form-validation branch from fbb15a0 to 8833b0d Compare February 23, 2025 12:59
This change implements all required “suffering from being missing”
constraints https://html.spec.whatwg.org/#suffering-from-being-missing
for HTMLInputElement.
@sideshowbarker sideshowbarker force-pushed the client-side-form-validation branch from 8833b0d to 915934f Compare February 23, 2025 14:33
@@ -561,11 +561,49 @@ unsigned HTMLFormElement::length() const
return elements()->length();
}

// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#statically-validate-the-constraints
HTMLFormElement::StaticValidationResult HTMLFormElement::statically_validate_contraints()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: contraints -> constraints

Comment on lines +573 to +574
VERIFY(is<FormAssociatedElement>(*element));
auto& field = dynamic_cast<FormAssociatedElement&>(*element);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 lines can be simplified to auto& field = as<FormAssociatedElement>(*element);

@@ -2525,8 +2525,7 @@ WebIDL::ExceptionOr<void> HTMLInputElement::step_up_or_down(bool is_down, WebIDL
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity
WebIDL::ExceptionOr<bool> HTMLInputElement::check_validity()
{
dbgln("(STUBBED) HTMLInputElement::check_validity(). Called on: {}", debug_description());
return true;
return dynamic_cast<FormAssociatedElement&>(*this).check_validity();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this method gets renamed, the cast should become unnecessary.

@@ -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
WebIDL::ExceptionOr<bool> FormAssociatedElement::check_validity()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to rename this method to check_validity_steps() to avoid the method being hidden. This would also match the name used in the spec.

I don't think there's any need for this to return ExceptionOr since there's no way it can error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants