Open jfsalaza opened 5 years ago
A few options:
<Formik validationSchema={values => {
let schemaShape = {}
if (values.thing) {
return Yup.object().shape({
thing: Yup.string().required()
profileOtherThing: Yup.string().required()
})
}
// otherwise do something else
return Yup.object().shape({/* ... */})
}}/>
validate
function that uses Yup and perhaps Yup.schema.validateAt() + some biz logic. You will likely want to utilize this helper method too https://github.com/jaredpalmer/formik/blob/master/src/Formik.tsx#L890@jaredpalmer On another note I think validationSchema = { values => {
does not pass through the values
. validate = { values => {
will do though.
Is it possible to separate the Form into smaller forms and submit them all at once ?
I'm trying to build a form with Formik but I'm unable to do the type of conditional validation that I need for my schemas. I have a Formik form where I'm trying to fill out an object called profile. The object looks something like this, this is what I have in my Formik initialValues:
Here is how my schema looks like:
Keep in mind that I also have 4 other subschemas at that level, but I removed them for simplicity. The other subschemas are called profileBarAssociationsSchema, profileJurisdictionSchema, profileLanguagesSchema, profileIndustrySchema. They all have up to 6 fields that need to be filed out.
So far this is what I've tried:
I wrote a function called requiredIf():
And its called inside a test for each field that I want to have validated:
But I get an error everything I tried to add a new field to the FieldArray and start filling out the second item instead of the first one.
Another thing I've tried was to use Yup.when but I have so many fields that it is hard to keep track of all the conditional validation combinations, that makes it almost impossible .
My question is how can I configure my ClientCreateAccountSchema so that the sub schemas are validated separately from each other? In other words, how can I my schema only validate the respective subchema when touched and not activate the other subschema? Is this something I can do with Yup? or is this something with Formik?
What other suggestions do you guys have?