我试图要求在表单中选择两个复选框之一。根据文档,以下内容似乎不起作用:
yearGroups: yup.object().shape({
primary: yup.bool().when('yearGroups.primary', {
is: false,
then: yup
.bool()
.oneOf([true], '')
.required()
}),
secondary: yup.bool().when('yearGroups.secondary', {
is: false,
then: yup
.bool()
.oneOf([true], '')
.required()
}),
}),
最佳答案
试试这个。参见说明here :
const schema = yup.object().shape({
yearGroups: yup.object().shape(
{
primary: yup.bool().when("secondary", {
is: (secondary) => !secondary,
then: yup.bool().oneOf([true], "At least one needs to be checked")
}),
secondary: yup.bool().when("primary", {
is: (primary) => !primary,
then: yup.bool().oneOf([true], "At least one needs to be checked")
})
},
[
["primary", "secondary"],
["secondary", "primary"]
]
)
});
现场演示
关于javascript - 是的,验证需要至少一项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69435280/