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

formStore.element.submit() causes page reload at Qwik #130

Closed
genki opened this issue Aug 26, 2023 · 14 comments
Closed

formStore.element.submit() causes page reload at Qwik #130

genki opened this issue Aug 26, 2023 · 14 comments
Assignees
Labels
enhancement New feature or request

Comments

@genki
Copy link

genki commented Aug 26, 2023

I would like the form to submit programmatically equivalent to the manual submit.
But I couldn't find the recommended way to do that in document.
So I call the formStore.element.submit() but it reload the page, although the form doesn't reload when to submit manually.
How we can correctly submit the form programmatically with validation and without to reload?

@fabian-hiller
Copy link
Owner

Did you check if the reload can be prevented by an argument or event.preventDefault()?

@fabian-hiller fabian-hiller self-assigned this Aug 26, 2023
@fabian-hiller fabian-hiller added the question Further information is requested label Aug 26, 2023
@genki
Copy link
Author

genki commented Aug 26, 2023

Sorry for less information.
I made a runnable example here
https://stackblitz.com/edit/modular-forms-qwik-p4noyg?file=src%2Froutes%2Flogin%2Findex.tsx

  1. Fill the form and click submit button then the dialog will appear.
  2. Focus on the textarea and hit the Command+Enter or Ctrl+Enter then the page will reload.

I would like to simulate the first behaviour for the second case. Is there a way to do that?

@fabian-hiller
Copy link
Owner

I don't know. Probably I would not use the "submit" function of the <form /> element and call a function directly instead.

@genki
Copy link
Author

genki commented Aug 26, 2023

I see. If I call the function directly, how can I validate the form?

@genki
Copy link
Author

genki commented Aug 26, 2023

For example, this comment form here is possible to submit by hitting Command+Enter with validation.
I would like to have this function for the modular-forms.

@fabian-hiller
Copy link
Owner

You can call validate: https://modularforms.dev/qwik/api/validate

@genki
Copy link
Author

genki commented Aug 31, 2023

I see, I will use it. Thank you :)

@genki genki closed this as completed Aug 31, 2023
@genki
Copy link
Author

genki commented Aug 31, 2023

@fabian-hiller I have tried to call the validate() but found an issue that the setFieldErrors() and setErrorResponse() are not exported so that we can't set the issues to the form.
How about to make the submit(formStore) method in order to simulate the equivalent action of the manual form submit.

@genki genki reopened this Aug 31, 2023
@fabian-hiller
Copy link
Owner

Oh, yes that's right. I will think about it. Until then, I recommend using validate with setResponse.

@fabian-hiller fabian-hiller added enhancement New feature or request and removed question Further information is requested labels Aug 31, 2023
@genki
Copy link
Author

genki commented Aug 31, 2023

Thanks :)
I've made this for workaround.
https://gist.github.com/genki/632d57ffc53a136521f486867756469b

@genki genki closed this as completed Aug 31, 2023
@katywings
Copy link
Contributor

I tried to simplify the workaround a bit 😅, here is mine:

// Old-school solution:
// "cancelable" has to be true. It allows modular-forms to preventDefault that "submit".
form.element.dispatchEvent(new Event('submit', { cancelable: true, bubbles:true }));

// Or for modern browsers
form.element.requestSubmit();

@fabian-hiller Let me know if you are interested in a PR for a submit helper with one/both of these patterns.

Related discussion:
final-form/react-final-form#878 (comment)

requestSubmit Browsersupport: https://caniuse.com/mdn-api_htmlformelement_requestsubmit

@fabian-hiller
Copy link
Owner

form.element.requestSubmit();

Does this work without specifying a submitter as the first argument?

@katywings
Copy link
Contributor

Yupp, in that case the form element itself is used as submitter :).

@fabian-hiller
Copy link
Owner

Ok, feel free to create a PR for a submit method. Contact me on GitHub or Discord if you have any questions. Currently I prefer to use .requestSubmit(), but I am open to other options.

katywings added a commit to katywings/modular-forms that referenced this issue Sep 21, 2023
fabian-hiller added a commit that referenced this issue Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants