Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation for Rate component ant.design

I'm trying to validate Rate component, set initial value, etc.

  {getFieldDecorator('rating', {
    initialValue: dataSource.getIn(['data', 'rating'], ''),
    rules: [{ required: true, message: 'Field is required' }],
  })(<Rate allowClear={false} onChange={this.setRating} />)}

Having code like this in my form result in an error after each value change

Warning: getFieldDecorator will override value, so please don't set value directly and use setFieldsValue to set it.

Any ideas how to overcome it? Code works as expected

like image 315
Denis Rybalka Avatar asked May 17 '26 01:05

Denis Rybalka


2 Answers

To validate <Rate /> in antd design you should use null as initial value.

<Form.Item>
    {getFieldDecorator('rating', {
        initialValue: review.rating ? review.rating:null,
        rules: [
            {   
                required: true,
                message:'Rating Required',
            },
        ],
    })(
        <Rate />,
    )}
</Form.Item>
like image 62
Majedur Avatar answered May 19 '26 13:05

Majedur


I think the reason is because you should not use onChange when your component is being controlled by the Form

if you would want to get the actual rate using: this.props.form.validateFields() since will be also do the field validation

like image 21
Yichaoz Avatar answered May 19 '26 14:05

Yichaoz