蚂蚁设计。如何动态设置表单字段错误消息?

时间:2017-11-12 07:11:46

标签: antd

表单字段有很多异步检查规则,因为合成的api可以通过返回不同的结果一次检查这些规则,我不想激发这么多的api请求。

3 个答案:

答案 0 :(得分:16)

语法在v4

中进行了更新

新语法是:

setFields.   |  Set fields status   |   (fields: FieldData[]) => void

示例:

form.setFields([
   {
     name: 'field-to-update',
     errors: ['error-string'],
   },
]);

答案 1 :(得分:8)

使用form.setFields

语法

Function({ fieldName: { value: any, errors: Error } })

来自here -

的示例
this.props.form.setFields({
  user: {
    value: values.user,
    errors: [new Error('forbid ha')],
  },
});

答案 2 :(得分:0)

当您需要添加自定义错误消息时,只需使用validateStatus && help属性。 例如,道具中出现loginError(字符串)错误:

 <Form.Item
    { ...props.loginError && {
        help: props.loginError,
        validateStatus: 'error',
    }}>
    {getFieldDecorator('email', {
        rules: [
            { required: true, message: 'You have to write the email' },
        ],
    })(
        <Input size="large"/>,
    )}
</Form.Item>
相关问题