validationSchema change depending on form values
See original GitHub issue❓Question
How can I change validationSchema depending on the state of my form?
The docType validation change depending on the value of my select.
I wanted to do this if validationSchema gave me the form props as arguments but it don’t.
Example:
<Formik
initialValues={{ planType: 'PRE' }}
validationSchema={props => {
const isCPF = props.values.docType === 'cpf'
return yup.object().shape({
docType: yup.string(),
number: isCPF ? yup.string().cpf() : yup.string().cnpj()
})
}}
onSubmit={loadPlanActivation}
render={this.renderForm}
/>
I could solve this with a ref or adding a personal onChange to setState and be able to have the value but it does not seem natural.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:7 (1 by maintainers)
Top Results From Across the Web
How to set validation schema based on condition using Yup ...
Try to validate like: name: yup.string().required(), extra: yup.array() .of( yup.object().shape({ required: yup.bool(), value: ...
Read more >Validation - Formik
Form -level validation is useful because you have complete access to all of your form's values and props whenever the function runs, so...
Read more >Dynamic Formik Validation - DEV Community
This gets called as a prop from the subform which passes the new initial values and validation schema up the main form. The...
Read more >How to Create an Optional Dynamic Validation Schema based ...
I encountered a scenario where sections of a form I was making were optional if omitted but required if they existed. Essentially the...
Read more >Validation - VeeValidate
A Field component represents a single form input and can be used to render ... When the validation schema changes, only the fields...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone facing this issue, we can do something like
Basically, we can use
You can just use the Yup when? See: https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema