问题描述
我想提出两个条件:如果isProduct
或isBox
为真,则product_id
应该是必需的。我在下面执行了此代码,但它不起作用
product_id: yup.string().when(['isProduct', 'isBox'], {
is: true,
then: yup.string().required('Select product'),
}),
推荐答案
当前,您正在检查两个字段是否都为真,若要检查其中一个字段为真,您需要重写IS属性以函数返回布尔值:
product_id: yup.string().when(['isProduct', 'isBox'], {
is: (isProduct, isBox) => isProduct || isBox,
then: yup.string().required('Select product'),
}),
引用:https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema