I have created a form using Ant Design with Reactjs. It works great except it shows error messages that I have specified for each field under the input field before the "Submit" button is even clicked and user is typing in. It looks very odd and I would like to change that behavior. I want to show these messages only after the submit button is clicked. Is there a way to do it?
I went through the official doc. I couldn't find anything regarding "async validator" or "getFieldDecorator" that suggested what I wanted to achieve.
The "getFieldDecorator" to specify validation rules and error messages for each field is set in this way:
<Form.Item label="Title" style={{ margin: 0 }}>
{getFieldDecorator('Title', {
rules: [{ required: true, message: 'Please input the title', whitespace: true }], initialValue: title
})(<Input onChange={this.onTitleChange.bind(this)} value={title} />)}
</Form.Item>
My "OnSubmit()" method looks like this:
handleSubmit = () => {
this.props.form.validateFields((err, values) => {
if (err) {
return;
}
this.handleSaveBanner();
});
};
Expected is to show error messages only after the submit button is clicked and not before that even if user types invalid inputs or leaves it blank. Help would be appreciated since I am new to React.
You have to click the "+ Add Field" button for the dynamic form to add and show the first field as shown here..
valuePropName is the name of the prop of the child component that will contain the value. In most cases, components such as Input or Select use value as the prop for holding the value. However, certain components such as Checkbox or Switch use checked .
In general, form validation errors fall into one of these groups: 1) a user entered an invalid data into a form field, for example, an email without "@" character.
wrapperCol: It is used to denote the layout for input controls.
Please look through this. https://ant.design/components/form/#getFieldDecorator(id,-options)-parameters.
There is a option named validateTrigger
. It can control when to do validation.
Wish it may hlp you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With