我有以下内容:
export const isOneFieldValid = (val: string) => {
console.log(val)
return val ? undefined : true
}
...
const validate = (field: string) => {
switch (field) {
case 'email': {
return composeValidators(isOneFieldValid, isValidEmail, hasStringeMaxLength)
}
case 'workPhone':
case 'homePhone':
case 'mobile': {
return composeValidators(isOneFieldValid, isNumber, hasNumberMaxLength)
}
default:
return undefined
}
}
...
这将验证所有4个字段,但是有一种方法可以假设所有字段均为空而仅验证一个字段?
只要有一种联系用户的方式,我就可以提交表格
答案 0 :(得分:1)
听起来像您需要表单级别的验证,而不是字段级别的验证吗?
<Form
onSubmit={onSubmit}
validate={values => {
const errors = {}
if (values.email && isValidEmail && hasStringMaxLength) {
return ;
} else {
errors.email = 'Require';
}
if (writeFunctionToCheckWorkPhone) {
return;
} else {
error.workPhone = 'Require';
}
if (writeFunctionToCheckHomePhone) {
return ;
} else {
error.homePhone = 'Require';
}
if (writeFunctionToCheckMobile) {
return ;
} else {
error.mobile = 'Require';
}
return errors;
}}
...