![](/img/trans.png)
[英]Yup validation, helperText added to MaterialUI Input Component
[英]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.