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

Prevent Double Submit #206

Closed
robodna opened this issue May 19, 2024 · 8 comments
Closed

Prevent Double Submit #206

robodna opened this issue May 19, 2024 · 8 comments
Assignees
Labels
question Further information is requested

Comments

@robodna
Copy link

robodna commented May 19, 2024

What is the 'ModularForms' way of preventing double-form submits?

Thank you,

@fabian-hiller
Copy link
Owner

Can you describe in more detail what you mean by "double-form submits"?

@fabian-hiller fabian-hiller self-assigned this May 19, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label May 19, 2024
@robodna
Copy link
Author

robodna commented May 19, 2024

The user double-clicks the 'submit' button. The second click should be ignored while the first submit is being processed, until the submit either returns an error or success.

Best,

@fabian-hiller
Copy link
Owner

fabian-hiller commented May 19, 2024

You can disable the submit button while formState.submitting is true: https://modularforms.dev/qwik/api/FormStore

You can "watch" the form state in our playground: https://modularforms.dev/qwik/playground/login

@robodna
Copy link
Author

robodna commented May 19, 2024

I did think of that but it does not prevent from the form being submitted multiple times by pressing 'enter'. It would be good to have a 'pending' form signal, or maybe adding a 'pending' property to form.response.status

@fabian-hiller
Copy link
Owner

I did think of that but it does not prevent from the form being submitted multiple times by pressing 'enter'.

Are you sure about that?

@robodna
Copy link
Author

robodna commented May 20, 2024

Yes, if you have the focus on an input and press enter, it will submit the form. I've added an external signal to solve the issue.

UPDATE: As you hinted, pressing enter twice in the input field does not submit the form twice and treats the action the same as a 'double click' on an <input tag set to 'submit' instead of 'button'. In my case, I had an 'onkeypress' event detecting 'enter' on a textarea to submit the form which could potentially cause a double-submit but is a unique issue to how I implemented the form. I fixed it and modular forms works as expected.

@fabian-hiller
Copy link
Owner

fabian-hiller commented May 20, 2024

I would expect that adding disabled={formState.submitting} to the button would disable any submission while there form is submitting. But maybe I'm wrong.

@robodna
Copy link
Author

robodna commented May 20, 2024

I think disabling the <button type="submit" prevents double-submits for double-clicks on the button and double-enter in an input field. I suspect adding an onKeypress handler detecting the 'enter' keypress on a textarea input and having that handler call 'myform.submit' in JS would still cause a double-submit. As you suggest, adding if (!formState.submitting) myForm.submit in the onKeypress event is the right solution in that case. All good now, thanks.

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

No branches or pull requests

2 participants