Ant Design 表单自定义验证器

Ant Design Form Custom Validator

在 ant design 中,可以提供如下所示的自定义验证器:

<Form.Item label="First Name">
  {getFieldDecorator("firstName", {
    rules: [
      {
        validator: (rule: any, value: string, cb: (msg?: string) => void) => {
          value.length < 3 ? cb("too short") : cb();
        }
      }
    ]
  })(<Input />)}
</Form.Item>

如您所见,我正在使用 typescript,因为它的转译器非常酷,它希望我也使用 validatorrule 参数。我找不到关于它的任何文档,也不知道有什么用。那么,如果可以的话,请简要解释一下它是什么以及应该如何使用它?

作为 Validation Rules 的一部分 validator 接受 rules 作为第一个参数。

由于它是 async-validator, you can check the Rules 规范的包装器:

function(rule, value, callback, source, options)

rule: The validation rule in the source descriptor that corresponds to the field name being validated. It is always assigned a field property with the name of the field being validated.

您也可以设置断点并查看其值是否符合您的需要。