Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Formik onSubmit never runs when the form is submitted

ErrorClicking Submit does nothing and onSubmit is never called

Short answer

Formik runs onSubmit only after validation finds no errors. When the submit does nothing, log formik.errors first: a hidden validation error aborts the submit silently. Then check that handleSubmit is on the form element and that every field has an initial value. Reset isSubmitting if onSubmit is not async.

  • Applies to Formik 2, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print Formik's errors next to the form. If the object is not empty, submission is being aborted.

    <pre>{JSON.stringify({ errors: formik.errors, isSubmitting: formik.isSubmitting, submitCount: formik.submitCount }, null, 2)}</pre>
  2. 2Match what you see, then jump to its fix

    1. 1errors is not emptyValidation errors abort the submissionFix
    2. 2Nothing happens on click and submitCount stays 0handleSubmit is not attached to a formFix
    3. 3Console warns about an input changing from uncontrolled to controlledA field is missing from initialValuesFix
    4. 4isSubmitting stays true and the submit button is disabled afterwardsonSubmit never signals completionFix

What does the error look like?

  • Clicking Submit does nothing and onSubmit is never called
  • The submit button stays disabled after the first click

Cause 1 of 4

Validation errors abort the submission

1Check

According to the Formik submission guide, when validation reports errors, submission is aborted, errors are stored and isSubmitting returns to false.

console.log(formik.errors)

2Fix

Make initialValues, the validation schema and the field names the same set, and show each error so none is hidden.

<Formik
  initialValues={{ email: '', password: '' }}
  validationSchema={schema}
  onSubmit={handleSave}
>

3Verify

Submit with valid data. onSubmit runs and errors stays empty.

console.log(formik.errors)

Confirmed by formik.org

Did this fix it?

Cause 2 of 4

handleSubmit is not attached to a form

1Check

Formik's API reference says handleSubmit should be passed to the form's onSubmit.

grep -rn "handleSubmit\|type=\"submit\"" src/

2Fix

Wrap the fields in a form element with onSubmit={formik.handleSubmit} and give the button type="submit".

<form onSubmit={formik.handleSubmit}>
  {/* fields */}
  <button type="submit">Save</button>
</form>

3Verify

Click the button; submitCount goes to 1.

console.log(formik.submitCount)

Confirmed by formik.org

Did this fix it?

Cause 3 of 4

A field is missing from initialValues

1Check

The Formik reference says every field must be initialised in initialValues, otherwise React raises the uncontrolled-to-controlled error.

console.log(formik.values)

2Fix

Add an initial value for every field name used by an input.

initialValues={{ name: '', age: 0, agree: false }}

3Verify

formik.values lists every field before the user types.

console.log(formik.values)

Confirmed by formik.org

Did this fix it?

Cause 4 of 4

onSubmit never signals completion

1Check

Formik resets isSubmitting by itself only if onSubmit returns a promise. A synchronous onSubmit has to call setSubmitting(false).

grep -rn "onSubmit" src/

2Fix

Return a promise from onSubmit (make it async), or call setSubmitting(false) when done.

onSubmit={(values, { setSubmitting }) => {
  save(values);
  setSubmitting(false);
}}

3Verify

Submit twice in a row; the second click is accepted.

console.log(formik.isSubmitting)

Confirmed by formik.org

Did this fix it?

How do I stop it coming back?

  • Render errors for every field while building the form.
  • Keep field names, initialValues and schema keys identical.
  • Make onSubmit async when it saves data.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.