>我有以下内容:
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 个字段,但是有没有办法只验证一个字段,假设它们都是空的?
只要有一种方法可以联系用户,我就可以提交表格
听起来您需要表单级验证,而不是字段级?
<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;
}}
...