【问题标题】:yup validation input dependence是的,验证输入依赖
【发布时间】:2021-09-07 21:12:23
【问题描述】:

大家好,我有以下验证:

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(),
              }),
        })
      ),
    })
  ),

我想要做的是,如果用户将在 question 输入中输入某些内容,其他输入应该可以正常工作,required() 方法。

我尝试以下:

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

但它不起作用, Unhandled Rejection (TypeError): Cannot read property 'length' of undefined 你能帮我解决这个问题吗?

【问题讨论】:

    标签: javascript reactjs formik yup


    【解决方案1】:

    您只需在 when 之前删除 required()

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

    【讨论】:

    • 感谢您的评论,但仍然无法正常工作Unhandled Rejection (TypeError): Cannot read property 'length' of undefined
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2012-04-03
    • 2019-12-11
    相关资源
    最近更新 更多