AntDesignPro表单自定义校验validator

高飞翮
2023-12-01
<ProFormText
  width="md"
  name="email"
  label="电子邮箱"
  placeholder="请输入电子邮箱"
  rules={[
    {
      required: true,
      message: '请输入电子邮箱',
    },
    {
      type: 'email',
      message: '请输入正确的电子邮箱',
    },
    {
      validator: (rule,value,callback) => {
        isEmailRepeatfn(value,callback);
      }
    }
  ]}
/>
const isEmailRepeatfn = (value: any, callback: any) => {
    getValidatorsUserEmail({email: value}).then((res:any) => {
      if(res.data.existed){
        callback('邮箱已存在!')
        return;
      }
      //必须总是返回一个 callback,否则 validateFieldsAndScroll 无法响应
      callback();
    })
  }
 类似资料: