Formik 验证模式是的,什么时候是 "answer" 或 "another answer"
Formik validation schema Yup , when is "answer" or "another answer"
基本上我想做的就是说如果 region = "Europe" 或 "other" 那么该字段是必需的,确实查看了 formik 文档但没有找到任何东西。
<Formik
enableReinitialize={true}
initialValues={{
name: currentBankData.name || '',
address: currentBankData.address || '',
country: currentBankData.country || '',
region: currentBankData.region || '',
city: currentBankData.city || '',
swiftCode: currentBankData.swiftCode || '',
routeCode: currentBankData.routeCode || '',
}}
validationSchema={Yup.object().shape({
name: Yup.string().min(3).required('Name is required.'),
address: Yup.string().required('Address is required.'),
country: Yup.string().required('Country is required.'),
region: Yup.string().required('Region is required.'),
city: Yup.string().required('City is required.'),
swiftCode: Yup.string().when('region', {
is: 'Europe' || 'other, //Would like to do something like this but
doesnt work :)
then: Yup.string()
.required('SwiftCode is required.')
.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
),
}),
routeCode: Yup.string().when('region', {
is: 'USA',
then: Yup.string().required('RouteCode is required.'),
}),
verified: Yup.bool(),
})}
使用 mixe oneOf
试试这个
validationSchema={Yup.object().shape({
name: Yup.string().min(3).required('Name is required.'),
address: Yup.string().required('Address is required.'),
country: Yup.string().required('Country is required.'),
region: Yup.string().required('Region is required.'),
city: Yup.string().required('City is required.'),
swiftCode: Yup.string().when('region', {
is: oneOf(['Europe', 'Europe', 'other'] as const),
then: Yup.string()
.required('SwiftCode is required.')
.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
),
}),
routeCode: Yup.string().when('region', {
is: 'USA',
then: Yup.string().required('RouteCode is required.'),
}),
verified: Yup.bool(),
})}
Finally Works
validationSchema={Yup.object().shape({
name: Yup.string().required('Name is required.'),
address: Yup.string().required('Address is required.'),
country: Yup.string().required('Country is required.'),
region: Yup.string().required('Region is required.'),
city: Yup.string().required('City is required.'),
swiftCode: Yup.string().when('region', (region, schema) => {
return ['Europe', 'other'].includes(region)
? schema
.required('SwiftCode is required.')
.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
)
: schema.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
);
}),
routeCode: Yup.string().when('region', {
is: 'USA',
then: Yup.string().required('RouteCode is required.'),
}),
verified: Yup.bool()
基本上我想做的就是说如果 region = "Europe" 或 "other" 那么该字段是必需的,确实查看了 formik 文档但没有找到任何东西。
<Formik
enableReinitialize={true}
initialValues={{
name: currentBankData.name || '',
address: currentBankData.address || '',
country: currentBankData.country || '',
region: currentBankData.region || '',
city: currentBankData.city || '',
swiftCode: currentBankData.swiftCode || '',
routeCode: currentBankData.routeCode || '',
}}
validationSchema={Yup.object().shape({
name: Yup.string().min(3).required('Name is required.'),
address: Yup.string().required('Address is required.'),
country: Yup.string().required('Country is required.'),
region: Yup.string().required('Region is required.'),
city: Yup.string().required('City is required.'),
swiftCode: Yup.string().when('region', {
is: 'Europe' || 'other, //Would like to do something like this but
doesnt work :)
then: Yup.string()
.required('SwiftCode is required.')
.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
),
}),
routeCode: Yup.string().when('region', {
is: 'USA',
then: Yup.string().required('RouteCode is required.'),
}),
verified: Yup.bool(),
})}
使用 mixe oneOf
试试这个validationSchema={Yup.object().shape({
name: Yup.string().min(3).required('Name is required.'),
address: Yup.string().required('Address is required.'),
country: Yup.string().required('Country is required.'),
region: Yup.string().required('Region is required.'),
city: Yup.string().required('City is required.'),
swiftCode: Yup.string().when('region', {
is: oneOf(['Europe', 'Europe', 'other'] as const),
then: Yup.string()
.required('SwiftCode is required.')
.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
),
}),
routeCode: Yup.string().when('region', {
is: 'USA',
then: Yup.string().required('RouteCode is required.'),
}),
verified: Yup.bool(),
})}
Finally Works
validationSchema={Yup.object().shape({
name: Yup.string().required('Name is required.'),
address: Yup.string().required('Address is required.'),
country: Yup.string().required('Country is required.'),
region: Yup.string().required('Region is required.'),
city: Yup.string().required('City is required.'),
swiftCode: Yup.string().when('region', (region, schema) => {
return ['Europe', 'other'].includes(region)
? schema
.required('SwiftCode is required.')
.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
)
: schema.matches(
/[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
'This is not the correct Swift Code'
);
}),
routeCode: Yup.string().when('region', {
is: 'USA',
then: Yup.string().required('RouteCode is required.'),
}),
verified: Yup.bool()