0

I have a condition that a certain field is required if the value is either '1', '2', '3', '4'. How can you do this? Pls check my code below

 travelDate: yup.string().when('planeAvailable', {
    is: '1' || '2' || '3' || '4',
    then: yup.string().required('Choose Date'),
  }),
Joseph
  • 7,042
  • 23
  • 83
  • 181

1 Answers1

0

It should be

 travelDate: yup.string().when('planeAvailable', {
    is: val => ['1', '2', '3', '4'].includes(val)
    then: yup.string().required('Choose Date'),
  }),

doc

is conditions are strictly compared (===) if you want to use a different form of equality you can provide a function like: is: (value) => value == true.

hgb123
  • 13,869
  • 3
  • 20
  • 38