Formik: Use multiple form with one submit button

Created on 11 Mar 2019  路  8Comments  路  Source: formium/formik

Question

I have one page with multiple form. Each form is independent from other.
My question is how to handle all form submitting with one button??

Needs More Information stale

Most helpful comment

@edwinwong90 thanks bro,

can you please do validation and error handling with this to use **Yup**
also can we update state on handleSubmit instead of onChange

All 8 comments

I had the same requirement, you can extract the Formik and Form tags in common file and pass enableReinitialize={true} to your tag. This way you can render the form fields conditionally, you just have to make sure that on changing view you have to pass the view-specific initialValues instance to Formik tag.

Code snippet I used to handle this situation:

render() {
let currentStep = this.props.steps[this.state.currentStepIndex];
const CurrentStepComponent = currentStep.component;

return (<Formik
                        enableReinitialize={true}
                        initialValues={currentStep.initialValues}
                        validationSchema={currentStep.validationSchema}
                        onSubmit={(values, { setSubmitting }) => {
                            setSubmitting(true);
                            // Deal with current form-view values.
                            setSubmitting(false);
                        }}
>
            {CurrentStepComponent && <CurrentStepComponent {...formik} />}
             {
                     // Common submit button can be placed here.
             }
</Formik>)
}

Hope this is helpful for you.

More info pls and/or code example

@edwinwong90 Nice job! How would you do validation and error handling with this?

@edwinwong90 thanks bro,

can you please do validation and error handling with this to use **Yup**
also can we update state on handleSubmit instead of onChange

I have the same question!

Closing due to long inactivity

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jungwoo-An picture Jungwoo-An  路  3Comments

najisawas picture najisawas  路  3Comments

ancashoria picture ancashoria  路  3Comments

giulioambrogi picture giulioambrogi  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments