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.

Conditional validation

See original GitHub issue

@jaredpalmer : Thanks for this great library. It’s much better than redux-form.

Btw, I have one question for the following form:

screenshot from 2017-07-09 22-05-17

Each state has a name and a bool field named no_cost. If no_cost = true, there’s no incurring cost.

I know Yup support conditional validation, but would you pls tell me how to apply it with formik?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
rostero1commented, Jul 9, 2017

Untested:

validationSchema: Yup.object().shape({
  name: Yup.string().required(),
  no_cost: Yup.boolean(),
  cost: Yup.number().when('no_cost', {
    is: true,
    then: Yup.number()
      .required('cost is required'),
    otherwise: Yup.number(),
  })
})
2reactions
jaredpalmercommented, Aug 30, 2018

@DavidCasillasRivero to avoid an arrow function debate 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 ways to implement conditional validation of Angular reactive ...
Another way to achieve conditional validation in Angular forms would be to write your own group validator. This validator will check the value...
Read more >
Conditional required validation in angular reactive form
I want to apply conditional validation on some properties based on some other form values. I have referred some answers Angular2: ...
Read more >
Reactive Forms Benefits: Conditional Validation - Preston Lamb
Reactive Forms Benefits: Conditional Validation ... easily add or remove validation for a given field based on the value of another field.
Read more >
Conditional Validation in Reactive Forms - Codementor
Validating your application form before reaching out to the server enhances security and performance. In this post, I will be showing you how...
Read more >
Conditional form validation with Angular Reactive Forms
In this tutorial you will learn how to use Angular Reactive Forms to implement conditional field validation in an Ionic/Angular application.
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