formik icon indicating copy to clipboard operation
formik copied to clipboard

Please make onSubmit/handleSubmit optional for use cases that don't involve submitting forms

Open nickmccurdy opened this issue 3 years ago • 21 comments

🚀 Feature request

Current Behavior

The Formik component's onSubmitprop is required. Failing to use it causes a runtime error when submitting and a TypeScript error (if you use it). You can work around this by passing a no-op function (onSubmit={() => {}}), but I think it's misleading for beginners and makes it seem like you need some specific code inside onSubmit.

Desired Behavior

Formik should allow the onSubmit prop to be omitted. Similarly, withFormik's handleSubmit option should be optional.

Suggested Solution

Make the type optional in TypeScript (using undefined or ?: in the prop type) and disable any submitting functionality when onSubmit is not given. Any validation and other synthetic events that don't rely on submitting should still work.

Who does this impact? Who is this for?

Any form state management use case that doesn't involve submitting forms, for example:

  • Beginners learning how to use Formik without a server
  • Experienced users creating a prototype of an app that doesn't have a server implemented yet
  • Calculating results based on form fields immediately as the user changes/blurs fields (I have multiple calculator apps that do this, and it's my main use case for this feature)
  • Persisting a bunch of fields to local storage without having to manage form state manually

Describe alternatives you've considered

There could be another library for managing state of form fields that aren't for submitting, but personally I think Formik is good enough at that already (despite this issue) that it wouldn't be worth the effort.

Additional context

When I first learned about Formik, I avoided it because I saw the onSubmit prop and assumed I had to have some sort of REST/GRAPHQL server/service in order to use it. I didn't realize until later that I could just use it for local state management. I think making submit behavior optional would make it easier to learn and understand the purpose of Formik. For me the biggest advantage of Formik is managing form state, not submitting forms.

I'd be happy to try a pull request if there's interest in merging it, otherwise I might just maintain a fork.

nickmccurdy avatar Aug 10 '20 09:08 nickmccurdy

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

github-actions[bot] avatar Nov 20 '20 00:11 github-actions[bot]

This is still an issue and the associated pull request is still active

nickmccurdy avatar Nov 20 '20 08:11 nickmccurdy

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

github-actions[bot] avatar Jan 21 '21 01:01 github-actions[bot]

This is still an issue and the associated pull request is still active

nickmccurdy avatar Jan 23 '21 17:01 nickmccurdy

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

github-actions[bot] avatar Feb 24 '21 00:02 github-actions[bot]

This is still an issue and the associated pull request is still active

nickmccurdy avatar Mar 06 '21 00:03 nickmccurdy

please fix this issue !

Heisendev avatar Jun 09 '21 08:06 Heisendev

I would also like to see onSubmit being optional rather than required. In my particular case, I only need the state statement feature of Formik rather than handling submitting form.

ibrahim-delice avatar Jun 14 '22 14:06 ibrahim-delice

Agreed!

malbaugh avatar Jun 29 '22 11:06 malbaugh

Was just wondering why I have to use Formik's onSubmit function when I have my own. Please fix!

MSkauen avatar Jul 25 '22 15:07 MSkauen

+1

sebastiancarlsson avatar Sep 15 '22 09:09 sebastiancarlsson

+1

yasserzubair avatar Oct 10 '22 23:10 yasserzubair

+1

isBellihi avatar Jan 03 '23 13:01 isBellihi

+1

barimehdi77 avatar Jan 20 '23 14:01 barimehdi77

Please thumbs up and optionally subscribe to my thread if you want to support it without commenting on anything specific.

Also, would the maintainers be interested in PRs for this?

nickmccurdy avatar Jan 20 '23 23:01 nickmccurdy

+1

lajerca avatar Apr 24 '23 11:04 lajerca

good

ahmadkybora avatar Aug 30 '23 06:08 ahmadkybora

I want to maintain form state management and flexibility while avoiding form submissions. This feature would be beneficial. 👍

rcanpahali avatar Nov 17 '23 10:11 rcanpahali

Is the PR still active or stale ?

shreykoradia avatar Feb 12 '24 14:02 shreykoradia

I'm not aware of a PR, and I didn't want to start one without confirmation that a maintainer would review it. Additionally the repository hasn't merged an external feature PR in 6 months.

nickmccurdy avatar Feb 12 '24 21:02 nickmccurdy

I'm not aware of a PR, and I didn't want to start one without confirmation that a maintainer would review it. Additionally the repository hasn't merged an external feature PR in 6 months.

I see

shreykoradia avatar Feb 13 '24 05:02 shreykoradia