首页 > 解决方案 > 为什么 yup 验证在匹配特定值时不起作用?

问题描述

我有一个场景,我必须btext根据另一个字段的 ( ) 值检查字段 ( aText) 值。仅当其他字段的值等于预定义的参考值 ( predefinedValue) 时,该检查才适用。下面是我的是的验证码

export const bText = yup
  .string()
  .when("aText", aText => {
    if (aText === predefinedValue) {
      console.log("its here");
      return yup.string().required("Required");
    } else return yup.string();
  });

当预定义值匹配bText且未填充时,验证有效。但即使bText被填满,验证也不会通过。我哪里错了?

标签: formikyup

解决方案


推荐阅读