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.

Yup conditional validation with Formik

See original GitHub issue

I’m trying to make a field “old_password” required only if another filed “password” is filled with some value.

It works when using the .when() function in Yup, but when the other field “password” gets empty, the “old_password” still gives me the error message that it is required, even though the “password” is empty.

Also when I implement this, the “name” field validation is not working anymore for some reason. And it works when I type in the “password” field 😕

I don’t know how to solve this.

Here’s a sample code …

https://codesandbox.io/s/wizardly-frost-sq7fq

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

41reactions
akmjenkinscommented, Jan 4, 2020

Formik sets the field to undefined when it’s empty (interesting). So you actually are throwing an error in your validation schema (property length does not exist on undefined). You need to change you condition to look like this:

        old_password: Yup.string().when("password", {
            is: value => value && value.length > 0, <- this line
            then: Yup.string().required(
                "Old password is required when setting new password"
            ),
            otherwise: Yup.string()
        })
6reactions
akmjenkinscommented, Dec 3, 2020

@jtterra, you’re kind of blocked from doing it “properly” by this.

But you can still do it, the thing is, you’re going to recreate your schema every time your state changes, so you don’t actually have a conditional schema per se, but you’re going to recreate your schema every time your state changes. This could be bad for perf (but if you don’t notice anything then it’s fine). In the meantime, keep an eye on the PR I mentioned above.

The way to do this is to put the schema as part of your state, and then update the schema every time hasNote changes. In the functional component world it would look like this:

const [hasNote,setHasNote] = useState(false);
const [schema,setSchema] = useState(() => FormSchema(hasNote));

useEffect(() => {
  // every time hasNote changes, recreate the schema and set it in the state
  setSchema(FormSchema(hasNote));
},[hasNote]);

return (<Formik validationSchema={schema} .../>)

and now FormSchema looks like this:

const FormSchema = (hasNote: boolean) => Yup.object().shape({
  // no more conditional schema, just recreating a schema every time hasNote changes
  note: hasNote ? Yup.string().required() : Yup.string()
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional validation with Yup and Formik - Stack Overflow
Conditional validations with YUP : ; 1. Single value, simple condition : RULE: Only ask for personName when isPerson true. ; 2. Single...
Read more >
Yup conditional validation with Formik - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
How To Do Conditional Validation With Formik and Yup
Formik and Yup provides a nice easy way to do conditional validation for your React projects. Using the when method you can conditionally...
Read more >
Conditional validation of form fields using Yup
How to enable or disable validation on Formik form fields based on certain conditions in Yup.
Read more >
Validation - Formik
Because we ❤️ Yup sooo much, Formik has a special config option / prop for Yup object schemas called validationSchema which will automatically...
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