32

A form field has many asynchronous check rules, since a composited api can check these rules one time by return different result, i do not want to fire so many api request.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
zheng li
  • 501
  • 1
  • 4
  • 10

4 Answers4

55

the syntax is updated in v4

new syntax is:

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

example :

form.setFields([
   {
     name: 'field-to-update',
     errors: ['error-string'],
   },
]);
BilluBaziger
  • 820
  • 8
  • 17
34

Use form.setFields

Syntax

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

Example from here -

this.props.form.setFields({
  user: {
    value: values.user,
    errors: [new Error('forbid ha')],
  },
});
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
A G
  • 21,087
  • 11
  • 87
  • 112
9

When you need to add a custom error message just use validateStatus && help attributes. For example, you've got an error as loginError (string) in your props:

<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>
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
ybaturin
  • 119
  • 1
  • 2
  • 4
0
                form.setFields([
                  {
                    name: "email", // required
                    value: "myemail@@gmail.com",//optional
                    errors: ["Invalid email"],
                  },
                ]);

I used it in v4.16.11