是的,React 中的 Formik 验证

Yup and Formik Validation in React

我有一个条件,如果值为“1”、“2”、“3”、“4”,则某个字段是必需的。你怎么能这样做?请检查下面的代码

 travelDate: yup.string().when('planeAvailable', {
    is: '1' || '2' || '3' || '4',
    then: yup.string().required('Choose Date'),
  }),

应该是

 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.