0
votes

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'),
  }),
1

1 Answers

0
votes

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.