antd 2.x form组件ref不能获取表单实例?

发布于 2022-09-13 00:36:02 字数 1366 浏览 14 评论 0

//排序
  sortModalSubmit = async () => {
    const { examInfo, subjectInfo, type } = this.state;
    console.log('ref', this.sortForm);
    const values = await this.sortForm.validateFields(); // TypeError this.sortForm.validateFields is not a function 这里报错
    const params = { ...values, id: type === 'examType' ? examInfo.id : subjectInfo.id, type };
    let res = request('/api/online-school/admin/exam/sort', {
      method: 'POST',
      body: JSON.stringify(params),
    })
 
  }

<Modal maskClosable={false} title="排序" visible={true} onOk={this.sortModalSubmit}
            onCancel={() => {
              this.setState({ sortVisible: false })
            }} bodyStyle={{ display: 'flex' }}>

            <Form
              ref={form => {this.sortForm = form}}
              >
              <Form.Item
                {...formProps}
                label="排序序号"
                required
              >
                
                {getFieldDecorator('sortNum', {
                })(<InputNumber min={1} />)}
              </Form.Item>
            </Form>
</Modal>

我用的react版本是"react": "^15.5.4","antd": "^2.12.3",这个react文档获取类组件ref就是这样写的.不过antd文档没有给这方面的说明.

请问我要怎么获取form表单的实例,来手动触发validateFields()拿到表单中的值
因为页面有很多model弹窗所以存在多个表单组件

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少跟Wǒ拽 2022-09-20 00:36:02

官方2.x文档

// antd2.x版本中的form 使用的是高阶函数来传递一个form实例
const WrappedNormalLoginForm = Form.create()(NormalLoginForm);
// 所有 你需要的form实例
可以直接使用 this.props.form 来获取。
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文