Formik: Conditional field rendering?

Created on 2 Sep 2018  路  6Comments  路  Source: formium/formik

Hey, im new to using formik and i just wanted to know if it was possible to do conditional field rendering. I need to show different questions depending on a select dropdown value. Doing this normally is quite easy i know. Just wanted to know if its possible to do it with Formik as im trying to create a multi step booking form and i want to use Formik to handle all my form state. 馃憤

Question

Most helpful comment

This is true, but if you're using validationSchema, you'll also have put conditional logic in the schema (i.e. the value for key conditionalField should be required only if it's currently being displayed). Do you have a suggestion on a good way to handle this without duplicating code?

All 6 comments

Formik doesn't take control over rendering your inputs--that's why it's powerful. You are in complete control of all rendering logic.

This is true, but if you're using validationSchema, you'll also have put conditional logic in the schema (i.e. the value for key conditionalField should be required only if it's currently being displayed). Do you have a suggestion on a good way to handle this without duplicating code?

@pimlottc-gov I am also facing the same issue. If you have figured out something then do share your approach.

I believe conditionally showing a field by passing the Field a validate prop (instead of passing it to the Formik component) should work.

How about something to aid listing what fields are currently in the dom. Something like currentFields mentioned here.

I am still facing this issue, was anyone able to get around this? is the CurrentFields released as part of formik now @jaredpalmer ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredpalmer picture jaredpalmer  路  3Comments

outaTiME picture outaTiME  路  3Comments

giulioambrogi picture giulioambrogi  路  3Comments

jeffbski picture jeffbski  路  3Comments

ancashoria picture ancashoria  路  3Comments