我尝试使用when方法有条件地验证基于父值的嵌套对象,但我没有设法使其工作。
const firstStep = Yup.object().shape({
accountHolder: Yup.object().shape({
accountType: Yup.number()
.typeError("Required")
.required("Required"),
mailingAddress: Yup.object().shape({
address1: Yup.string().when("accountType", {
is: 2,
then: Yup.string()
.min(5, "Min 5 char")
.required("Required")
})
})
})
});发布于 2021-06-13 01:00:05
下面的方法将会起作用:
const firstStep = Yup.object().shape({
accountHolder: Yup.object().shape({
accountType: Yup.number()
.typeError("Required")
.required("Required"),
mailingAddress: Yup.object().when("accountType", {
is: 2,
then: Yup.object().shape({
address1: Yup.string()
.min(5, "Min 5 char")
.required("Required")
})
})
})
});https://stackoverflow.com/questions/55776103
复制相似问题