我正在嘗試通過以下驗證創建動態表單 -
- 用戶必須至少選擇 2 個選項。
- 每個選擇都必須是非空字串。
我正在嘗試使用 Yup 進行表單驗證來實作它。
這是 codesandbox 鏈接 - https://codesandbox.io/s/dynamic-form-using-react-hook-form-forked-mo4p79
我在這里面臨以下問題 -
即使用戶創建了 3 個選項,錯誤“至少需要 2 個選項”也不會消失。
首選輸入未得到驗證。
我創建了以下 formSchema 來強制執行最少 2 個元素和非空輸入 -
export const formSchema = yupResolver(
yup
.object({
choices: yup
.array(
yup.object().shape({
value: yup.string().required("This field is required.")
})
)
.min(2, "Minimum of 2 choices are required!")
})
.required()
);
每次單擊提交按鈕后它都可以正常驗證但它不是動態驗證。
uj5u.com熱心網友回復:
我自己解決了。問題是由于使用外部驗證庫是的。
檔案中提到,當前 fieldArray 的驗證規則僅適用于內置驗證。
Important: This is only applicable to built-in validation only
這是固定沙箱的鏈接 - CodeSandBox
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/535783.html
