issue: Separate forms produces conflict and call each other's onSubmits #12186
Replies: 10 comments
-
We can also do :
// in riginal form onSubmit:
|
Beta Was this translation helpful? Give feedback.
-
You are effectively nesting forms, either don't nest your form by moving the modal out of the form or put a wrapper in with stop propagation. |
Beta Was this translation helpful? Give feedback.
-
If I use react-portals for my modal, doesn't my modal renders in a whole other element? so It's not actually nested. |
Beta Was this translation helpful? Give feedback.
-
React will propagate the events by default through your portal, so it effectively is nested. That is why I have stopped propagation on my modal. |
Beta Was this translation helpful? Give feedback.
-
But they are not nested. they are siblings. propagation goes up to the DOM. It doesn't go to sibling nods |
Beta Was this translation helpful? Give feedback.
-
In the example you posted it is nested in the react code. |
Beta Was this translation helpful? Give feedback.
-
come on  ̄へ ̄ |
Beta Was this translation helpful? Give feedback.
-
OK I will stop answering then. I had read it but as you are not linking to your code but only your example I can only give you feedback on the example. |
Beta Was this translation helpful? Give feedback.
-
Had the same exact issue today. The forms are not nested ( can be seen in the DOM tree ) but both forms are submitting at the same exact time. Thanks to @MajidBabaeifar for the solution, had to tweak it a little bit but eventually it worked fine for me. This has to be fixed for the future though |
Beta Was this translation helpful? Give feedback.
-
@EWisselink as per your suggestion I moved the form that is inside a separate modal component into the body element, to ensure that both forms are not nested. However, upon submitting that modal form, the main form gets submitted as well. I found a workaround for that, that prevents the propagation of the default events onSubmit={e => {
e?.stopPropagation();
e.preventDefault();
return handleSubmit(onSubmit)(e);
}} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Version Number
7.52.0
Codesandbox/Expo snack
https://codesandbox.io/embed/p7p6lx24xq?codemirror=1
Steps to reproduce
The sandBox link is just a demonstration of something like the UI I have and IS NOT my project.
Further explanation :
After trying some ways (preventDefault and stopPropagation and ... ) some struggling, I did a hacky way to prevent this:
and ...
Which works when in the modal fields doesn't have validation errors. but when they have error and the onSubmit of the modal doesn't run because of validation error, then it doesn't run the above code and therefore original onSubmit gets called again.
Expected behaviour
Calling modal form should not have any effects on the other one.
What browsers are you seeing the problem on?
Chrome
Relevant log output
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions