簡體   English   中英

是的,驗證輸入依賴

[英]yup validation input dependence

大家好,我有以下驗證:

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您能幫我解決這個問題嗎?

您只需在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