Wrong value if multiple submit buttons used
See original GitHub issueDescribe the bug I’m not sure if this is a bug or is it how it supposed to be but when I have two or more submit buttons and I want to get the value of the clicked button I always get the first button value.
To Reproduce Steps to reproduce the behaviour:
- Go to https://codesandbox.io/s/react-hook-form-get-started-forked-3u9t1
- Open the console
- Click on SUBMIT 1 button
- Console logs:
{example: "test", submitValue: "button1"}
good - Click on SUBMIT 2 button
- Console logs:
{example: "test", submitValue: "button1"}
wrong, expected{example: "test", submitValue: "button2"}
Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-get-started-forked-3u9t1
Expected behaviour
Data in onSubmit
callback should have correct value based on which button user clicked.
Desktop (please complete the following information):
- OS: macOS Mojave (10.14.6)
- Browser Chrome
- Version 90.0.4430.212 (Official Build) (x86_64)
Thanks for creating this amazing library!
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
css - Multiple submit buttons on HTML form – designate one ...
The problem here is that when a user hits Enter to submit the form, the post variable "COMMAND" contains "Prev"; normal, as this...
Read more >Form api treats multiple submit buttons wrong. | Drupal.org
If two submit buttons have same values, the first button will be treated as the second button.. Any help will be appreciated!
Read more >[Mac] Invalid form params when multiple submit buttons are ...
On Mac devices (Safari or Firefox), however, always the value of the first submit button is part of the Params hash. Chrome on...
Read more >Multiple submit buttons and JavaScript - Sarbbottam's weblog
Two sets of action can happen as per users' desire, guided by the respective submit buttons. Might be a confusing user experience for...
Read more >How to use multiple submit buttons in an HTML form
How to use multiple submit buttons in an HTML form ? · Create a form with method 'post' and set the value of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, that’s one way to go.
In case someone ends up here from Google, I actually solved it in a bit different way. Instead of using
data
parameter fromonSubmit
I usednativeEvent
ssubmitter
property like so:And then added submit buttons without registering them in
react-hook-form
at all.If you think for a second about it, you probably will realise that if you only have yes/no questions through the entire wizard you probably don’t even need to use
react-hook-form
. You can just simply handle submission + state population manually without any library.In my case yes/no are actually just one type of a form step (I simplified it for sake of showcasing the problem). Some steps in my wizard are fully-fledged forms, and only some are yes/no, so I’ll keep
react-hook-form
for all steps for consistency.Here’s the working demo: https://codesandbox.io/s/react-hook-form-get-started-forked-jk5ym
And once again @bluebill1049 thanks for the support and your hard work!
thanks, @dawidurbanski for the follow-up and detailed explanation. This will definitely be useful for others. 👍