Jer*_*ews 9 javascript reactjs yup formik
我正在使用 formik 进行表单验证,并在数组验证中遇到了一些问题。这是我的表单结构
{
flow: [
{ text: "hello"
},
{ input: "world"
},
{ buttons: [
'hi',
'hello'
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我必须为此创建验证架构。所以数组可能包含这些对象中的任何一个。
我试过这个,
export const validationSchema = yup.object().shape({
flow: yup.array().of(
yup.mixed().oneOf([
{
text: yup.string().required('Enter text'),
},
{
buttons: yup.array().of(yup.string().required('Enter button title')),
},
{
input: yup.string(),
}
])
),
});
Run Code Online (Sandbox Code Playgroud)
但我收到以下 formik 错误:
flow:[
"flow[0] must be one of the following values: [object Object], [object Object]",
"flow[1] must be one of the following values: [object Object], [object Object]"
]
Run Code Online (Sandbox Code Playgroud)
怎么解决这个问题呢?
Chr*_* B. -1
如果您查看该oneOf方法的函数签名:
mixed.oneOf(arrayOfValues: Array<any>, message?: string | function): Schema
Run Code Online (Sandbox Code Playgroud)
第一个参数是 的数组any,因此第一个参数的数组中的任何值都有效。第二个message参数只能是字符串/函数。
话虽这么说,您的代码看起来确实正确,所以我不确定为什么您会收到任何错误。也许尝试添加一个消息参数,看看是否可以安抚事情。如果有的话,您收到的具体错误是什么?
| 归档时间: |
|
| 查看次数: |
12388 次 |
| 最近记录: |