Two Condition in When in Yup in React

13.4k views Asked by At

I wanted to put two conditions that if its isProduct or isBox is true, then product_id should be required. I did this code below and it isnt working

 product_id: yup.string().when(['isProduct', 'isBox'], {
    is: true,
    then: yup.string().required('Select product'),
  }),
1

There are 1 answers

5
Dipen Shah On BEST ANSWER

Currently, you are checking if both fields are true, in order to check either of the fields is true, you need to override is property to function returning boolean value:

product_id: yup.string().when(['isProduct', 'isBox'], {
  is: (isProduct, isBox) => isProduct || isBox,
  then: yup.string().required('Select product'),
}),

Ref: https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema