使用 Yup 和 Formik 进行条件验证
Conditional validation with Yup and Formik
这是我的验证模式:
const validationSchema = Yup.object().shape({
person: Yup.object().shape({
name: Yup.string().required('Field is required'),
surname: Yup.string().required('Field is required'),
middleName: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required')
}),
company: Yup.object().shape({
name: Yup.string().required('Field is required'),
address: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required')
})
});
而且 React State 中还有两个变量:isPerson
和 isCompany
。如何使验证有条件地工作,例如如果 isPerson
为真,则需要验证 validationSchema
中的 person
?
您可以像任何其他对象一样有条件地添加到您的验证模式:
let validationShape = {
company: Yup.object().shape({
name: Yup.string().required('Field is required'),
address: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required')
})
};
if (this.state.isPerson) {
validationShape.person = Yup.object().shape({
name: Yup.string().required('Field is required'),
surname: Yup.string().required('Field is required'),
middleName: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required');
}
const validationSchema = Yup.object().shape(validationShape);
Updated ans: 2020.
你可以使用 Yup 条件
const validationSchema = Yup.object().shape({
isCompany: Yup.boolean(),
companyName: Yup.string().when('isCompany', {
is: true,
then: Yup.string().required('Field is required')
}),
companyAddress: Yup.string().when('isCompany', {
is: (isCompany) => true,//just an e.g. you can return a function
then: Yup.string().required('Field is required'),
otherwise: Yup.string()
}),
});
并确保相应地更新您的表单。我希望你明白这一点...
email: Yup.string()
.when([‘, 'showEmail’, ’anotherField’], {
is: (showEmail, anotherField) => {
return (showEmail && anotherField);
},
then: Yup.string().required('Must enter email address')
}),
也可以使用多个字段进行验证。
虽然公认的解决方案有效,但它有一个问题 - 要验证的两个字段很常见,因此必须重复。就我而言,我的大部分字段只有 2-4 个异常值。
所以这是另一个解决方案:
分别定义每个模式 - 即 3 个模式 - commonSchema 用于公共字段,personSchema 用于个人特定字段,companySchema 用于公司特定字段。
根据状态合并模式
const validationSchema = isPerson
? commonSchema.concat(personSchema)
: commonSchema.contact(companySchema)
有关 'concat' 的详细信息,请参阅 github 上的 docs。
使用 YUP 的条件验证 :
when可以做的各种验证如下:
1.单值,简单条件:
规则:仅当 isPerson 为真时才询问 personName。
personName : string().when('isPerson', {
is: true,
then: Yup.string().required('Field is required'),
otherwise: Yup.string(),
})
2。单值,复杂条件:
规则:只有当公司是“IT”时才会询问 personName。
personName : string().when('company', {
is: (company)=> company==='IT',
then: Yup.string().required('Field is required'),
otherwise: Yup.string(),
})
3。多值,复杂条件:
规则:仅在公司为“IT”且人员有效时询问人员姓名。
personName : string().when(['company', 'isPerson'], {
is: (company,isPerson)=> company==='IT'&& isPerson,
then: Yup.string().required('Field is required'),
otherwise: Yup.string(),
})
这是我的验证模式:
const validationSchema = Yup.object().shape({
person: Yup.object().shape({
name: Yup.string().required('Field is required'),
surname: Yup.string().required('Field is required'),
middleName: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required')
}),
company: Yup.object().shape({
name: Yup.string().required('Field is required'),
address: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required')
})
});
而且 React State 中还有两个变量:isPerson
和 isCompany
。如何使验证有条件地工作,例如如果 isPerson
为真,则需要验证 validationSchema
中的 person
?
您可以像任何其他对象一样有条件地添加到您的验证模式:
let validationShape = {
company: Yup.object().shape({
name: Yup.string().required('Field is required'),
address: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required')
})
};
if (this.state.isPerson) {
validationShape.person = Yup.object().shape({
name: Yup.string().required('Field is required'),
surname: Yup.string().required('Field is required'),
middleName: Yup.string().required('Field is required'),
email: Yup.string()
.email('Wrong e-mail format')
.required('Field is required');
}
const validationSchema = Yup.object().shape(validationShape);
Updated ans: 2020.
你可以使用 Yup 条件
const validationSchema = Yup.object().shape({
isCompany: Yup.boolean(),
companyName: Yup.string().when('isCompany', {
is: true,
then: Yup.string().required('Field is required')
}),
companyAddress: Yup.string().when('isCompany', {
is: (isCompany) => true,//just an e.g. you can return a function
then: Yup.string().required('Field is required'),
otherwise: Yup.string()
}),
});
并确保相应地更新您的表单。我希望你明白这一点...
email: Yup.string()
.when([‘, 'showEmail’, ’anotherField’], {
is: (showEmail, anotherField) => {
return (showEmail && anotherField);
},
then: Yup.string().required('Must enter email address')
}),
也可以使用多个字段进行验证。
虽然公认的解决方案有效,但它有一个问题 - 要验证的两个字段很常见,因此必须重复。就我而言,我的大部分字段只有 2-4 个异常值。
所以这是另一个解决方案:
分别定义每个模式 - 即 3 个模式 - commonSchema 用于公共字段,personSchema 用于个人特定字段,companySchema 用于公司特定字段。
根据状态合并模式
const validationSchema = isPerson ? commonSchema.concat(personSchema) : commonSchema.contact(companySchema)
有关 'concat' 的详细信息,请参阅 github 上的 docs。
使用 YUP 的条件验证 :
when可以做的各种验证如下:
1.单值,简单条件:
规则:仅当 isPerson 为真时才询问 personName。
personName : string().when('isPerson', {
is: true,
then: Yup.string().required('Field is required'),
otherwise: Yup.string(),
})
2。单值,复杂条件:
规则:只有当公司是“IT”时才会询问 personName。
personName : string().when('company', {
is: (company)=> company==='IT',
then: Yup.string().required('Field is required'),
otherwise: Yup.string(),
})
3。多值,复杂条件:
规则:仅在公司为“IT”且人员有效时询问人员姓名。
personName : string().when(['company', 'isPerson'], {
is: (company,isPerson)=> company==='IT'&& isPerson,
then: Yup.string().required('Field is required'),
otherwise: Yup.string(),
})