id | title |
---|---|
form-submission |
Form Submission |
To submit a form in Formik, you need to somehow fire off the provided handleSubmit(e)
or submitForm
prop. When you call either of these methods, Formik will execute the following (pseudo code) each time:
- Touch all fields.
initialValues
are required and should always be specified. See #445 - Set
isSubmitting
totrue
- Increment
submitCount
+ 1
- Set
isValidating
totrue
- Run all field-level validations,
validate
, andvalidationSchema
asynchronously and deeply merge results - Are there any errors?
- Yes: Abort submission. Set
isValidating
tofalse
, seterrors
, setisSubmitting
tofalse
- No: Set
isValidating
tofalse
, proceed to "Submission"
- Yes: Abort submission. Set
- Proceed with running the submission handler (i.e.
onSubmit
orhandleSubmit
) - Did the submit handler return a promise?
- Yes: Wait until it is resolved or rejected, then set
isSubmitting
tofalse
- No: Call
setSubmitting(false)
to finish the cycle
- Yes: Wait until it is resolved or rejected, then set
How do I determine if my submission handler is executing?
If isValidating
is false
and isSubmitting
is true
.
Why does Formik touch all fields before submit?
It is common practice to only show an input's errors in the UI if it has been visited (a.k.a "touched"). Before submitting a form, Formik touches all fields so that all errors that may have been hidden will now be visible.
How do I protect against double submits?
Disable whatever is triggering submission if isSubmitting
is true
.
How do I know when my form is validating before submit?
If isValidating
is true
and isSubmitting
is true
.
Why does `isSubmitting` remain true after submission?
If the submission handler returns a promise, make sure it is correctly resolved or rejected when called.
If the submission handler does not return a promise, make sure setSubmitting(false)
is called at the end of the handler.