问题描述
我想要一些关于这样的场景的最佳实现的信息:我有一个选择器和两个日期选择器。 当我从 select (idRisk) 参数 PRESENT 中选择时,这两个日期必须是必需的。 如果 DateA 在 DateB 之前,则在表单中我想显示验证错误。 实现这一目标的最佳方法是什么? 我写这个是为了完成,但显示这个错误=错误:循环依赖,节点是:“dateA”
validationSchema: Yup.object({
idRisk: Yup.number().required(),
dateB: Yup.mixed().when("idRisk", {
is: Risk.PRESENT,
then: Yup.mixed().required(),
otherwise: Yup.mixed()
}),
dateA: Yup.mixed().when("idRisk", {
is: Risk.PRESENT,
then: Yup.mixed().required(),
otherwise: Yup.mixed()
})
.when(["dateA", "dateB"],
(dateA, dateB) => {
if (dateA.isBefore(dateB))
return this.required();
}
)
})
1楼
Mike D3ViD Tyson
1
2020-02-14 15:40:23
您不能在dateA:Yup.mixed().when()方法中引用参数["dateA"] ,请考虑使用不同的方法,例如:
dateA: Yup.mixed()
.when(["dateB"],
(dateB, schema, node) => {
if (node.value.isBefore(dateB))
return this.required();
}
)
您可以从node.value获取 dateA 值