两种情况在什么时候在YUP中反应 [英] Two Condition in When in Yup in React

查看:0
本文介绍了两种情况在什么时候在YUP中反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出两个条件:如果isProductisBox为真,则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

这篇关于两种情况在什么时候在YUP中反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆