-
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
Would be nice if submit events exposed the "submitter" element #3195
Comments
Thanks. This seems pretty reasonable. I take it you'd be interested in implementing this in Firefox? @tkent-google, our usual Chrome forms person, is out this quarter, so I'm not sure we can give much help from Chrome's side on getting a second implementer. Perhaps @rniwa or @cdumez have thoughts from Safari, or @travisleithead from Edge. |
I suspect we'd want to at least look at implementing this, yes, if people like the idea. |
This would be great. The current workaround is probably either looking for the focused element (https://stackoverflow.com/questions/2066162/how-can-i-get-the-button-that-caused-the-submit-from-the-form-submit-event) or recording the target of click events until the next tick, just in case a submit event immediately follows the click event. |
I think this is reasonable. Chrome will implement this if the specification has it. |
Came here to post this issue if it wasn't already present. Firefox exposes the However, it doesn't entirely solve the problem. If we have the following form: <form>
<input type="text" name="title" />
<button type="submit" name="mode" value="publish">Publish</button>
<button type="submit" name="mode" value="draft">Save as Draft</button>
</form> And the user hits enter on the text box, it will submit the form with Edit: I suppose it's possible to check Another way to solve this problem, which is not mutually exclusive to the above, is to have a // event listener for form submit
function handleSubmit(e) {
e.preventDefault();
const formData = e.toFormData();
if (formData.get("mode") === "draft") {
...
} else {
...
}
} |
@paulirwin you can get that information from the Given that this would impact "construct the entry list" should we add The main thing blocking this at this point (aside from deciding where to add it) is a volunteer to drive the work. |
Thanks @annevk, did not know about that one. (MDN hasn't even been updated yet, and didn't find a relevant issue here before I posted initially.) In the docs for "Constructing the entry list", it does not specify if the Is the |
Interesting, it's not cancelable by design (the algorithm is also used by the I'm not sure why we decided to fire it after submit though, that might have mostly been an artifact from the structure of the algorithm before we made the order observable. @tkent-google what do you think, should we change that? |
I think it's possible to make |
/cc @muan who if I recall initially wanted formdata to be cancelable, but then found a more elegant way of coding the scenario in question. I am curious if @paulirwin's issue could be similar to what she encountered? |
@domenic Yes, but we talked about custom elements, not submitters (ref). This is what happens with our remote submit handler:
So come to think of it, this would be an issue for our remote submit handler as well. I guess we won't be able to drop our submit helper yet for In "constructing the entry list" it says:
So I guess there are two possibilities:
form.addEventListener('submit', event => {
event.preventDefault()
const formData = new FormData(form, event.howeverWeGetSubmitter)
fetch(form.action, {method: 'post', body: formData})
})
form.addEventListener('formdata', event => {
event.preventDefault()
fetch(form.action, {method: 'post', body: event.formData})
}) I don't know what implication 2. might have, but I think 1. makes more sense intention-wise and code changes would be more straightforward as we have a lot of other app code listening the submit event. cc @josh |
Apologies. My head is clearer now and I see that my example of catching So I think 1. is probably the way I'd see this work. Thoughts? |
Two other options, that aren't necessarily mutually exclusive to those proposed above:
|
@paulirwin 1 does not work as per @tkent-google above as we don't want to collect the form data until after this event, as this event can modify what is being submitted. 2 is rather interesting, but I worry a bit about clearing it appropriately across the various exit points of form submission. Now, a problem with adding a second parameter to the So adding state to |
It feels like the last few posts have been based on a false premise. Namely, the idea that the submitter element on submit events would be at all related to the proprietary Firefox-only explicitOriginalTarget, and thus could end up as an input element or similar. I don't think that's the proposal. The proposal is instead to expose the submitter element, which in the implicit submission case, would be the first submit button (i.e. the one with mode=publish in the given example). |
I agree that explicitOriginalTarget is unrelated, FWIW. The problem is how to expose the submitter element as per @muan's comments above. |
👋 We recently attempted to add
This sounds like a good solution to us too. With this approach I think we won't have to change our remote submit handler at all and can just delete our workarounds! 🎩 🐰 We recently open sourced our remote submit handler: https://github.com/github/remote-form. And here's a demonstration for the exact issue we are blocked on (test in Chrome for |
@tkent-google, would you have the bandwidth for a spec pull request and tests for this feature? It seems to have multi-implementer interest, as well as web developer interest, and you've been doing great work on forms features recently :). |
Somewhat relevant: I noticed a Safari specific behavior that seems to resemble what we're talking about here. The behavior is: " Please see the demo here: https://html-is.glitch.me/demo/formdata-entries.html Safari: Firefox/Chrome/Edge: |
I think I can start to work on this in two weeks. |
It has 'submitter' attribute. This fixes whatwg#3195
Specification PR: #4984 |
@tkent-google rereading the discussion here it's not immediately apparent why adding this to the And also, it would be good to get some insight into the behavior @muan mentions above in Safari. @cdumez perhaps? |
I think adding As for the Safari behavior, I think it's just a bug. Chrome inherited it from WebKit, and I fixed it three years ago for a user-reported issue. Adding another state to |
Thank you, that helps. The one wrinkle with the mentioned change to And the obvious alternative, letting the first argument be a dictionary to specify both Maybe this is something that only concerns me however. |
Hmm, I don't have an idea of a perfect solution. There are some options. A) B) C)
The content of the dictionary is idiomatic. D)
E) |
@annevk I agree with your assessments. It's a shame that the Safari behavior is a bug, but I understand that keeping this magic (submitter as a state) in browser is less ideal, and that exposing this would be better for developers. |
F) (no changes on the constructor)
Developers have to write |
I like A, or overloading. Overloading seems equivalent for authors, and in this particular case, better for spec people/implementers. See https://heycam.github.io/webidl/#idl-overloading-vs-union ; this is one of the few places where overloading is pretty applicable. Perhaps if we were designing things greenfield, there would be a |
During the review, the following question came up: If a <form> is submitted without a submit button and dispatches a 'submit' event, should the
|
Since they are functionally identical @muan thoughts? |
I agree that |
I'm happy to do null. But I would like us to change requestSubmit() to accept null too then, so you can do |
Ok, I'll update the PR for |
It has a submitter attribute, so this fixes #3195. As part of this, update requestSubmit() to accept null, so that requestSubmit(e.submitter) always works. (e.submitter is null for implicit submission, per the discussion in #3195 (comment).) Tests: web-platform-tests/wpt#19562
🎉 !!
Looks like #4984 does resolve this current issue. That leaves us still the |
I filed whatwg/xhr#262 for the submitter on |
It has a submitter attribute, so this fixes #3195. As part of this, update requestSubmit() to accept null, so that requestSubmit(e.submitter) always works. (e.submitter is null for implicit submission, per the discussion in #3195 (comment).) Tests: web-platform-tests/wpt#19562
See thread starting at https://lists.w3.org/Archives/Public/public-whatwg-archive/2017Oct/0013.html
Properly building the form submission requires knowing the submitter element. See https://html.spec.whatwg.org/#constructing-the-form-data-set and the "submitter" argument it takes.
If someone is trying to do the equivalent from a submit event, they can create a FormData from the form, but there's no way to figure out the submitter element at that point.
Ideally the submit event would expose the submitter element, if any, and the FormData constructor would take it as a second optional argument.
The text was updated successfully, but these errors were encountered: