antd的验证-在一个formitem中多input的验证方法?

FormItem中有多个input,每个都需要验证,写多个getFieldDecorator肯定不行,API里说了

一个 Form.Item 建议只放一个被 getFieldDecorator 装饰过的 child,当有多个被装饰过的 child 时,help required validateStatus 无法自动生成。

有什么好方法呢?

阅读 13.1k
3 个回答

自己想到一个解决方法:

formitem 中有help 和 validateStatus,
·
<FormItem help={this.state.help} validateStatus={this.state.validateStatus} >

<input1 onChange={....}>
<input2 onChange={....}>

</FormItem>
·
通过在onChange事件中修改state的help和validateStatus来显示错误信息或者正确结果。

<FormItem
    {...formItemLayout}
    label={'股东'}
>
    <Row>
        <Col span={10}>
            {this.props.form.getFieldDecorator('shareHolder3', {
                rules: [{required: true, message: '请输入股东名称'}],
            })(<Input />)}
        </Col>
        <Col span={10}>
            <FormItem>
                {this.props.form.getFieldDecorator('shareHolder4', {
                    rules: [{required: true, message: '请输入正确的持股比例'}],
                })(<Input />)}
            </FormItem>
        </Col>
    </Row>
</FormItem>

这样就可以让两个输入框在一行,并且分别对其验证!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题