formik - 条件验证是的 - 任何一个字段都是必需的(错误:未捕获的错误:循环依赖,节点为:“b”。)
问题描述
在我的应用程序中,我使用的是 Yup 验证。我遇到了一个场景,我至少需要三个字段(字符串)中的一个。我试过使用下面的代码,但它抛出Uncaught Error: Cyclic dependency, the node was: "b"。
a: yup.string().when(['b', 'c'], {
is: (b, c) => !b && !c,
then: yup.string().required()
}),
b: yup.string().when(['a', 'c'], {
is: (a, c) => !a && !c,
then: yup.string().required()
}),
c: yup.string().when(['a', 'b'], {
is: (a, b) => !a && !b,
then: yup.string().required()
})
}, [['a', 'b'], ['a', 'c'], ['b','c']])```
Any response or working code would be very helpful. Thanks in advance.
解决方案
我发现你可以使用lazy
Yup 中的构造来做到这一点。
懒惰的参考:https ://github.com/jquense/yup#yuplazyvalue-any--schema-lazy
创建在验证/转换时评估的架构。对于为多态字段和数组创建递归模式(如树)很有用。
例子:
a: yup.lazy(() => yup.string().when(['b', 'c'], {
is: (b, c) => !b && !c,
then: yup.string().required()
})),
b: yup.lazy(() => yup.string().when(['a', 'c'], {
is: (a, c) => !a && !c,
then: yup.string().required()
})),
c: yup.lazy(() => yup.string().when(['a', 'b'], {
is: (a, b) => !a && !b,
then: yup.string().required()
}))
}, [['a', 'b'], ['a', 'c'], ['b','c']])```
推荐阅读
- r - r 中具有 iid 随机效应的泊松 GLM 的奇怪输出
- postgresql - (psycopg2.ProgrammingError) 缺少表“t”的 FROM 子句条目
- reactjs - 使用样式化组件,有没有办法选择一个类名
- c# - 以默认排序顺序获取 OneDrive 项目
- c# - 如何解决这个设计问题:从抽象的类继承到接口?
- machine-learning - WEKA 输出中的缺失值
- javascript - 更新时,CODEIGNITER 相关下拉菜单未显示所选值
- javascript - 为什么 Javascript EventLoop 有不同的结果?
- android - 在 ScrollView 中更改滚动颜色
- python - 如何从多个设置值中获取产品 - Python