简体   繁体   English

Rate 组件 ant.design 的验证

[英]Validation for Rate component ant.design

I'm trying to validate Rate component, set initial value, etc.我正在尝试验证 Rate 组件、设置初始值等。

  {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.警告: getFieldDecorator会覆盖value ,所以请不要直接设置value并使用setFieldsValue来设置它。

Any ideas how to overcome it?任何想法如何克服它? Code works as expected代码按预期工作

I think the reason is because you should not use onChange when your component is being controlled by the Form我认为原因是因为当你的组件被Form控制时你不应该使用onChange

if you would want to get the actual rate using: this.props.form.validateFields() since will be also do the field validation如果您想使用以下this.props.form.validateFields()获得实际费率: this.props.form.validateFields()因为也将进行字段验证

To validate <Rate /> in antd design you should use null as initial value.要在antd design验证<Rate /> ,您应该使用null作为初始值。

<Form.Item>
    {getFieldDecorator('rating', {
        initialValue: review.rating ? review.rating:null,
        rules: [
            {   
                required: true,
                message:'Rating Required',
            },
        ],
    })(
        <Rate />,
    )}
</Form.Item>

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

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