0

I am using Formik and yup for form validation reactjs Javascript, I have 5 fields in the form and I need to validate at least any of the 2 fields should be filled so how can I achieve using yup library.

If no field is filled means above the form I am showing form feedback which I need to show error message with "Any two field is required"

For Example: Below example is validating only for any one field

a: yup.lazy(() => yup.string().when(['b', 'c'], 
{
 is: (b, c) => !b && !c,
 then: yup.string().required()
})),
b: yup.lazy(() => yup.string().when(['a', 'c'], {
 is: (a, c) => !a && !c,
 then: yup.string().required()
})),
c: yup.lazy(() => yup.string().when(['a', 'b'], {
 is: (a, b) => !a && !b,
 then: yup.string().required()
}))
}, [['a', 'b'], ['a', 'c'], ['b','c']])

I have followed this https://github.com/jquense/yup/issues/176 and Condition Validation Yup- Any one field is required (Error: Uncaught Error: Cyclic dependency, the node was: "b".)

Please help to achieve the approach Thank you

Anup
  • 67
  • 1
  • 1
  • 11

0 Answers0