question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

62reactions
ankurjoshi54commented, Jun 8, 2020

For anyone facing this issue, we can do something like

  <Formik
    initialValues={{ planType: 'PRE' }}
    validationSchema={() => yup.lazy((values) => {
      const isCPF = values.docType === 'cpf'

      return yup.object().shape({
        docType: yup.string(),
        number: isCPF ? yup.string().cpf() : yup.string().cnpj()
      })
    })}
    onSubmit={loadPlanActivation}
    render={this.renderForm}
  />

Basically, we can use

validationSchema={() => yup.lazy((values) => {
  // values is the formik.values object
  // return schema conditionally based on values object
})}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found