Formik: Yup default values integration

Created on 31 May 2018  路  5Comments  路  Source: formium/formik

Describe the solution you'd like
To extend Yup integration it would be neat to be able to pass a YUP schema into validationSchema and use its default values to set the initialValues object.

stale

Most helpful comment

Currently I do it like:

const schema =  yup.object({
    text: yup.string().required().default('some string'),
    flag: yup.bool().required().default(true),
});

const initialValues = schema.cast();

You can pass initialValues to Formik component, together with schema.

All 5 comments

This would be great for sure. I'm planning on looking into it further, and would be curious to know if anyone else knows if there's currently a way to do this

Currently I do it like:

const schema =  yup.object({
    text: yup.string().required().default('some string'),
    flag: yup.bool().required().default(true),
});

const initialValues = schema.cast();

You can pass initialValues to Formik component, together with schema.

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it.

@klis87 's solution works great for most cases, but in the case where the default value comes from a prop (e.g., edit forms where values are pre-populated), make sure to put the schema in the render() method to make those props accessible.

Was this page helpful?
0 / 5 - 0 ratings