是的,验证需要两个字段之一(其中一个是数字数组)
Yup validate either one of two fields is required (one of them is an array of numbers)
我将 Formik 与 Yup 和 Typescript 一起使用,并将其作为表单的初始值...
const initialValues = {
title: "",
overview: "",
related_items_id: [],
short_desc: ""
};
这是我的架构...
const formSchema = Yup.object().shape({
title: Yup.string()
.trim()
.required("This field is required."),
overview: Yup.string().required("This field is required."),
related_items_id: Yup.array()
.min(1, "Pick at least 1 item")
.of(Yup.number().required("This field is required.")),
short_desc: Yup.string().required("This field is required.")
});
现在,我需要 related_items_id
数组或 short_desc
是必需的,如果其中一个填充了数据,则另一个不需要,反之亦然,我如何使用某些东西来完成它喜欢 when
是吗?
这是我创建的一个代码框,用于显示我在尝试使用 Yup 的 when
方法时遇到的错误 ...
https://codesandbox.io/s/formik-yup-required-one-or-the-other-nextjs-gujqv
您可以通过创建一个在 related_items_id
和 short_desc
上相互依赖的类型来实现这一点
export interface BaseType {
title: string;
overview: string;
}
interface RelatedItemsType extends BaseType {
related_items_id?: Array<any>;
short_desc?: never;
}
interface ShortDescType extends BaseType {
related_items_id?: never;
short_desc?: string;
}
export type InitialValueType = RelatedItemsType | ShortDescType;
你可以像这样使用它
const initialValues: InitialValueType = {
title: "",
overview: "",
related_items_id: [],
// short_desc: "" no longer required
};
要有条件地设置您的 formSchema,请查看文档 Conditionally Set Required Field (Yup)。
const basicFormSchema = Yup.object().shape(
{
title: Yup.string()
.trim()
.required("This field is required."),
overview: Yup.string().required("This field is required."),
related_items_id: Yup.array().when("short_desc", {
is: "",
then: Yup.array()
.min(1, "Pick at least 1 item")
.of(Yup.number().required("This field is required.")),
otherwise: Yup.array()
}),
short_desc: Yup.string().when("related_items_id", {
is: relatedItemsId => relatedItemsId.length === 0,
then: Yup.string().required("This field is required."),
otherwise: Yup.string()
})
},
[["related_items_id", "short_desc"]]
);
这是检查是否至少完成其中一项的方法。
const schema = yup.object().shape({
'fieldOneName': Yup.string()
.when('fieldTwoName', {
is: (fieldTwo) => !fieldTwo || fieldTwo.length === 0,
then: Yup.string()
.required('At least one of the fields is required'),
}),
'fieldTwoName': Yup.string()
.when(codiceFiscale.name, {
is: (fieldOne) => !fieldOne || fieldOne.length === 0,
then: Yup.string().
.required('At least one of the fields is required'),,
})
}, ['fieldOneName', 'fieldTwoName']) // <-- HERE!!!!!!!!
<Formik
initialValues={{
email: '',
mobile: '',
submit: null,
}}
validationSchema = {
Yup.object().shape({
'email': Yup.string()
.when('mobile', {
is: (mobile) => !mobile || mobile.length === 0,
then: Yup.string()
.required('At least one of the fields is required'),
}),
'mobile': Yup.string()
.when('email', {
is: (email) => !email || email.length === 0,
then: Yup.string()
.required('At least one of the fields is required')
})
}, ['email', 'mobile'])
}
我将 Formik 与 Yup 和 Typescript 一起使用,并将其作为表单的初始值...
const initialValues = {
title: "",
overview: "",
related_items_id: [],
short_desc: ""
};
这是我的架构...
const formSchema = Yup.object().shape({
title: Yup.string()
.trim()
.required("This field is required."),
overview: Yup.string().required("This field is required."),
related_items_id: Yup.array()
.min(1, "Pick at least 1 item")
.of(Yup.number().required("This field is required.")),
short_desc: Yup.string().required("This field is required.")
});
现在,我需要 related_items_id
数组或 short_desc
是必需的,如果其中一个填充了数据,则另一个不需要,反之亦然,我如何使用某些东西来完成它喜欢 when
是吗?
这是我创建的一个代码框,用于显示我在尝试使用 Yup 的 when
方法时遇到的错误 ...
https://codesandbox.io/s/formik-yup-required-one-or-the-other-nextjs-gujqv
您可以通过创建一个在 related_items_id
和 short_desc
export interface BaseType {
title: string;
overview: string;
}
interface RelatedItemsType extends BaseType {
related_items_id?: Array<any>;
short_desc?: never;
}
interface ShortDescType extends BaseType {
related_items_id?: never;
short_desc?: string;
}
export type InitialValueType = RelatedItemsType | ShortDescType;
你可以像这样使用它
const initialValues: InitialValueType = {
title: "",
overview: "",
related_items_id: [],
// short_desc: "" no longer required
};
要有条件地设置您的 formSchema,请查看文档 Conditionally Set Required Field (Yup)。
const basicFormSchema = Yup.object().shape(
{
title: Yup.string()
.trim()
.required("This field is required."),
overview: Yup.string().required("This field is required."),
related_items_id: Yup.array().when("short_desc", {
is: "",
then: Yup.array()
.min(1, "Pick at least 1 item")
.of(Yup.number().required("This field is required.")),
otherwise: Yup.array()
}),
short_desc: Yup.string().when("related_items_id", {
is: relatedItemsId => relatedItemsId.length === 0,
then: Yup.string().required("This field is required."),
otherwise: Yup.string()
})
},
[["related_items_id", "short_desc"]]
);
这是检查是否至少完成其中一项的方法。
const schema = yup.object().shape({
'fieldOneName': Yup.string()
.when('fieldTwoName', {
is: (fieldTwo) => !fieldTwo || fieldTwo.length === 0,
then: Yup.string()
.required('At least one of the fields is required'),
}),
'fieldTwoName': Yup.string()
.when(codiceFiscale.name, {
is: (fieldOne) => !fieldOne || fieldOne.length === 0,
then: Yup.string().
.required('At least one of the fields is required'),,
})
}, ['fieldOneName', 'fieldTwoName']) // <-- HERE!!!!!!!!
<Formik
initialValues={{
email: '',
mobile: '',
submit: null,
}}
validationSchema = {
Yup.object().shape({
'email': Yup.string()
.when('mobile', {
is: (mobile) => !mobile || mobile.length === 0,
then: Yup.string()
.required('At least one of the fields is required'),
}),
'mobile': Yup.string()
.when('email', {
is: (email) => !email || email.length === 0,
then: Yup.string()
.required('At least one of the fields is required')
})
}, ['email', 'mobile'])
}