简体   繁体   English

是的,验证输入依赖

[英]yup validation input dependence

Hi all I have following validation:大家好,我有以下验证:

const questionAddValidation = Yup.object().shape({

  questions: Yup.array().of(
    Yup.object({
      question: Yup.string().required().min(100)
       
      level: Yup.string().required().when("question", {
                 is: (value) => value.length > 0,
                 then: Yup.string().required(),
              }),

      answers: Yup.array().of(
        Yup.object({
          answer: Yup.string().required().min(1).when("question", {
                 is: (value) => value && value.length > 0,
                 then: Yup.string().required(),
              }),
        })
      ),
    })
  ),

What I want to do is that if user will type something in question input for other inputs should work yup required() method.我想要做的是,如果用户为其他输入输入有question内容,则应该使用required()方法。

I try following:我尝试以下:

  is: (value) => value.length > 0

But it not working, Unhandled Rejection (TypeError): Cannot read property 'length' of undefined can u please help me to resolve this problem?但它不起作用, Unhandled Rejection (TypeError): Cannot read property 'length' of undefined您能帮我解决这个问题吗?

You just remove required() before when您只需在when之前删除required()

 level: Yup.string().when("question", {
    is: (value) => value.length > 0,
    then: Yup.string().required(),
 }),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM