简体   繁体   English

Ant Design 表单自定义验证器

[英]Ant Design Form Custom Validator

In ant design one can provide a custom validator like the following:在 ant 设计中,可以提供一个自定义验证器,如下所示:

<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>

As you see I'm using typescript and cause its transpiler is really cool it wants me to use rule parameter of validator as well.正如您所看到的,我正在使用 typescript 并导致它的转译器非常酷,它希望我也使用validator rule参数。 I can't find any documentation on it and don't know what is good for.我找不到任何关于它的文档,也不知道有什么用。 So if you can please explain briefly what is it and how it should be used?因此,请您简要解释一下它是什么以及应该如何使用它?

As part of Validation Rules validator accepts rules as first argument.作为验证规则的一部分,验证validator接受rules作为第一个参数。

Due to the fact it's a wrapper for async-validator , you can check the Rules specification:由于它是async-validator的包装async-validator ,您可以查看Rules规范:

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

rule: The validation rule in the source descriptor that corresponds to the field name being validated. rule:源描述符中与要验证的字段名称对应的验证规则。 It is always assigned a field property with the name of the field being validated.它总是被分配一个带有被验证字段名称的字段属性。

You also can put a breakpoint and see its value for your needs.您还可以放置一个断点并查看它对您的需求的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM