-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Missing reportValidity()
hook
#9878
Comments
Also, wrt FACE, the spec mentions a 3rd argument to |
There is also a use case where a custom element might want to report a valid validity. That would not be possible with only the |
I think the core issue here is that there does not appear to be a way to know when the browser will interactively validate the constraints of a form element. The |
Here's a proposal: in https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#interactively-validate-the-constraints, insert the following steps between 2 and 3:
That way a control can cancel the |
This looks like a good idea to me, and this is a bit of a sticking point around form validation; there isn't a great way to correctly suppress the browser native UI popovers. |
So as I understand it and to oversimplify:
I would also like to know when an element leaves a previously held invalid state. Stopping the popovers is good but you also need to display the error message to the user. A common way is a list below the input. Knowing when there's a new kind of constraint violation would let you know when to add an item to the list but not when to remove it. If any change to the set of constraints violations fires an event you could add and remove and decouple the "display errors" component from the input element. (There is still the annoyance of only being able to set a single custom constraint violation per element but that should be a separate issue). |
Not quite; there are 2 parts to client-side validation:
The first step is the Once the |
That is up to you as component author. If you use |
I want to be able to have a component that only exists to report error messages and whose only coupling to the element (custom or otherwise) it reports on is the changes in the constraint validations which it uses to show/hide the corresponding error message. For that to happen the error report element needs to know both when new constraint violations are triggered and when old constraint violations no longer apply. So if an element has 3 items and a minlength=4 there needs to be an event when the 4th item is added saying that I'm not sufficiently skilled at reading specs to tell if this change would allow that or not. |
@jpzwarte it sounds pretty reasonable to me to expose this. Another way of doing it might be that we don't make the other |
What is the issue with the HTML Standard?
Given a form containing both native elements (
<input>
s) and Form Associated Custom Elements, there are several ways to trigger the browser to show any validation messages:form.requestSubmit()
form.reportValidity()
input.reportValidity()
Calling this when the input is
:invalid
or after callingsetCustomValidity(message)
on it, this will trigger a native "popover" with the built-in or custom validation message.Each vendor has its own styling for these "popovers". So I'm trying provide my own styling which is the same across vendors. The easy part is to call
event.preventDefault()
on theinvalid
event. That event is dispatched when callingcheckValidity()
orreportValidity()
. By doing that, all vendors no longer show the native popover.The problem starts after that: there is no way to detect when a Form Associated Custom Element (or a web component wrapping an
<input>
for that matter) should display a validation message. The only event dispatched when callingreportValidity()
isinvalid
, but since that is also dispatched when callingcheckValidity()
, it cannot be used for that. Also, the event has no additional info to distinguish betweencheckValidity()
orreportValidity()
.Is there a reason such a "reportValidity() hook" is missing from the standard? What's the idea behind
checkValidity()
triggering aninvalid
event? (if it didn't, then we could just use theinvalid
event for this)In #5016 (comment) this was also mentioned and acknowledged that this is not possible.
The text was updated successfully, but these errors were encountered: