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.

Pass external variable to onSubmit

See original GitHub issue

Are you submitting a bug report or a feature request?

Feature request

What is the current behavior?

onSubmit only receives predefined arguments without an option to pass some custom data through handleSubmit() or form.submit()

What is the expected behavior?

It would be very useful if handleSubmit could accept more than one variable, and pass these variables to the onSubmit call.

Use case

Consider having a form with two actions: Save and Send to moderation

In both cases the form should first go through basic validation, then save the data, but if user has chosen to “Send to moderation”, we should make an additional validation check and require more fields, and if this validation passes - make one more api call.

It is currently possible to take the values from the form’s render props and make a custom callback. However, the only non-hacky way to throw submit errors is to return them from onSubmit. Therefore, unless we are able to pass some custom data, in this case - user’s action, we would not be able to achieve the desired behaviour without using some hacks like saving the action via the useRef before calling onSubmit.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:12

github_iconTop GitHub Comments

1reaction
BATCOHcommented, May 11, 2021

Another approach is to use some field as container for your custom option:

const handleSubmit = async ({ action, ...values }, form) => {
  if (action === "delete") {
    ...
  } else {
    ...
  }
}
...
<Form onSubmit={handleSubmit}>
  {({
    form: {
      change,
    },
    submitting,
  }) => (
    <DeleteButton
      type="submit"
      onClick={() => change("action", "delete")}
      disabled={submitting}
    >
      Delete
    </DeleteButton>
    <Button
      type="submit"
      onClick={() => change("action", "save")}
      disabled={submitting}
      isLoading={submitting}
      label="Save"
    />
  )}
</Form>

Yep, its not about refs, but you can pass some flag and make condition inside submit based on its value.

1reaction
alfondotnetcommented, Dec 15, 2020

You can make the Send to moderation button type=button which will not submit the form, but call your custom function which will run submit and call whatever additional logic is needed.

Can you provide an example? I’m stuck on this also

I think Something like this should work

const sendToModeration = (values) => {}; 
const submitForm = (values) => {}; 

return <Form onSubmit={(values) => {
 // default submit code
submitForm(values);
}>
  {({ handleSubmit, form: { getState } }) => <form onSubmit={handleSubmit}>
  <input type="submit" />
  <input type="button" onClick={() => sendToModeration(getState().values}} />
</form>}
</Form>
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to return a variable to a form onsubmit event?
you can declare a global variable for your function like this var flag=false;//here I've set default value as false function validate(){ .
Read more >
How onsubmit Event work in JavaScript? Examples - eduCBA
The onsubmit event is performed the triggers is based upon the submit function whenever the client user request in the form-wise data is...
Read more >
Passing a form variable to action onSubmit? - WebmasterWorld
- I have a form with an input field. - I need to pass the value of said input filed into the form's...
Read more >
onsubmit Event - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >
Javascript: prevent double form submission - The Art of Web
When the form passes validation the button text is changed to "Submitting form. ... and doing away with the necessity for global variables....
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