-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Proposal: allow a mode of form.submit() that behaves like clicking a submit button #4187
Comments
Thanks Domenic for introducing the proposal! To add a little more context, the hack is required because, even so we have a |
Seems like a good idea although maybe adding a new method would be cleaner since this is really a request to submit if the validation had passed. Another thing to consider is some aspects of it should be configurable. e.g. it's odd that the flag named interactive would trigger the firing of submit event. |
I think some of this is handled by |
It'd be nice if this can also support the case of having GitHub has a similar hacky |
@rniwa agreed! and adding a new method would also make it easier to detect when this new feature is available |
This sounds a good feature. I'll implement this in Chrome if this is standardized. |
I agree with @muan that this should also have a submitter argument, if we're going to expose this primitive anyway. The slight complication that brings is some verification that it's indeed an okay submit button for the form. |
Extending what @muan said, it would be great to be able to simulate what a: (tentative names) form.requestSubmit({
type: "reset", // defaults to "submit"
value: "somevalue", // defaults to undefined
name: "somename", // defaults to undefined
}) equivalent to: <button type="reset" name="somename" value="somevalue"> Submits without extra config: form.requestSubmit() equivalent to: <button type="submit"> thoughts about what this hypothetical method should return? |
Submitting is a primitive (taking a submitter), doesn't return anything, but you could imagine returning details about the resulting state: https://html.spec.whatwg.org/#concept-form-submit. Resetting is a different primitive (does not take arguments), also doesn't return anything: https://html.spec.whatwg.org/#concept-form-reset. I think that means we want two methods here. Return values could make sense, but would probably delay things somewhat as we'd have to make sure that the return value is what we want long term (returning undefined for now and upgrading later is also an option). |
@annevk you right!
absolutely, no need to make the proposal more complicated than it should be. There are already good ways to collect the submitted values (tentative) // equivalent to <input type="submit">
form.requestSubmit();
// equivalent to <input type="submit" name="somename" value="somevalue">
form.requestSubmit({
value: "somevalue",
name: "somename"
}); |
I'd make it take an actual element as that's the underlying primitive. |
This seems like a great feature. Glad it can fix hacky workarounds in at least GitHub and Ionic (and probably others). Edge team supports and would also be willing to ship. |
Now we have So, we should start with
It triggers submit with What do you think? |
Yeah, that's the shape I want, except the IDL should be (optional arguments should use undefined, not null in JavaScript): void requestSubmit(optional HTMLElement submitterButton); What if you don't pass a submit button (i.e., What about reset, we'd leave that alone for now? |
Looking at https://html.spec.whatwg.org/#concept-form-submit it seems like the assumption is that submitter is a form-associated element. I think that's probably the right restriction for now. Later, we can consider opening it up to form-associated custom elements as well, although for full parity it seems like we might need an additional way to say if the form-associated custom element "is a submit button", and thus its formmethod/formenctype/formaction attributes should be consulted.
I'm not sure there's any value in exposing #concept-form-reset vs. just the existing |
IMO, we should start with a stricter rule, and loosen it if necessary. So, the function should throw a
I think we can create the submit button behavior with the current form-associated custom element proposal and class SubmitElement extends HTMLElement {
...
handleClick() {
this.#internals.setFormValue(this.getAttribute('value'));
this.#internals.form.requestSubmit(this); // UA consults formmethod/formenctype/formaction attributes of this element.
this.#internals.setFormValue(null);
}
} |
That works for me. Good catch on checking its form owner; we should definitely do that.
So this would require first losening the requirement so that "the right control type" includes any form-associated custom element, right? This also doesn't allow the implicit submission behavior of https://html.spec.whatwg.org/#implicit-submission . But maybe that is a good thing; I think most web developers hate that feature. Otherwise I agree that this suffices. Very nice. Clever trick with the setting value before/after.
I missed |
Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597
Closes #4187. Tests: web-platform-tests/wpt#16743.
Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597
…), a=testonly Automatic update from web-platform-tests html: Add a test for form.requestSubmit() Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597 -- wp5At-commits: 1de2d96f75cdc7a920b74a8f406262f16531ef0f wpt-pr: 16743
…), a=testonly Automatic update from web-platform-tests html: Add a test for form.requestSubmit() Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597 -- wp5At-commits: 1de2d96f75cdc7a920b74a8f406262f16531ef0f wpt-pr: 16743
Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597
…), a=testonly Automatic update from web-platform-tests html: Add a test for form.requestSubmit() Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597 -- wp5At-commits: 1de2d96f75cdc7a920b74a8f406262f16531ef0f wpt-pr: 16743 UltraBlame original commit: d9fee4b7ddd070aaf762769226681d22b8473f86
…), a=testonly Automatic update from web-platform-tests html: Add a test for form.requestSubmit() Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597 -- wp5At-commits: 1de2d96f75cdc7a920b74a8f406262f16531ef0f wpt-pr: 16743 UltraBlame original commit: d9fee4b7ddd070aaf762769226681d22b8473f86
…), a=testonly Automatic update from web-platform-tests html: Add a test for form.requestSubmit() Specification issue: whatwg/html#4187 Specification PR: whatwg/html#4597 -- wp5At-commits: 1de2d96f75cdc7a920b74a8f406262f16531ef0f wpt-pr: 16743 UltraBlame original commit: d9fee4b7ddd070aaf762769226681d22b8473f86
Right now, the following snippets of code behave differently:
Per https://html.spec.whatwg.org/#concept-form-submit, the second one gets a lot more useful behaviors: interactive validation, submit events, and the potential of replacement-enabled navigation when the document is not yet completely loaded.
Wouldn't it be nice if we could do
or similar? (Bikeshed the name at your leisure.)
This is of special concern to folks trying to create custom form controls that behave like submit buttons, or for any attempts to explain the platform's elements in terms of imperative constructs.
Credit to @manucorporat for this idea; he pointed out that Ionic is currently using the hacky workaround for their custom submit button. That makes me sad and it would be nice if we could help them not do that.
/cc @tkent-google @rniwa @smaug---- @travisleithead for implementer thoughts.
The text was updated successfully, but these errors were encountered: