question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong value if multiple submit buttons used

See original GitHub issue

Describe 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:

  1. Go to https://codesandbox.io/s/react-hook-form-get-started-forked-3u9t1
  2. Open the console
  3. Click on SUBMIT 1 button
  4. Console logs: {example: "test", submitValue: "button1"} good
  5. Click on SUBMIT 2 button
  6. 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
dawidurbanskicommented, Jun 5, 2021

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 from onSubmit I used nativeEvents submitter property like so:

const onSubmit = (data, { nativeEvent }) => {
  const { submitter } = nativeEvent;

  actions.updateAction({
    dogs: submitter.value
  });

  props.history.push("./step2");
};

And then added submit buttons without registering them in react-hook-form at all.

<button type="submit" value="yes">
  yes
</button>
<button type="submit" value="no">
  no
</button>

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!

0reactions
bluebill1049commented, Jun 5, 2021

thanks, @dawidurbanski for the follow-up and detailed explanation. This will definitely be useful for others. 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found