假设我有这个初始值
{animal: "rhino", parts: {legs: 4, horns: 0}}
在我的yup模式中:
animal = yup.object().shape({
animal: yup.string().oneOf(["dog", "cat", "deer", "rhino"])
parts: yup.object().when("animal", value => {
switch(value) {
case "dog":
case "cat":
return yup.object().shape({
parts: yup.object().shape({
legs: yup.number().required()
})
});
case "deer":
default:
return yup.object().shape({
parts: yup.object().shape({
legs: yup.number().required(),
horns: yup.number().required()
})
});
}
})
});
每当我以动物dog
或cats
的形式提交表格时,horns
仍会包含在提交的数据中。有没有办法让yup或formik在提交时自动将其删除?