低代码编程实战平台BUILDER · 拖拽搭建
低代码编程实战平台 Low-Code Builder
BLD-230033 表单 2026.03.25 —

antd表单手动提交:掌握高效数据提交技巧,轻松实现表单操作与前端框架完美融合

Component
表单
Build
230033
Created
03.25
Updated
03.25

引言

antd 是一个由 React 实现的 UI 库,广泛应用于企业级产品的开发中。其中,antd 的表单组件提供了丰富的功能和便捷的使用方式。但在某些场景下,我们需要手动控制表单的提交过程,以满足特定的业务需求。本文将详细介绍如何使用 antd 表单手动提交,并探讨如何与前端框架完美融合。

1. antd 表单组件简介

在开始手动提交之前,我们先简要了解一下 antd 的表单组件。antd 的表单组件主要包括:

  • Form:表单容器,用于创建和管理表单项。 -FormItem:表单项容器,用于包裹输入框、选择框等表单元素。
  • Input、Select、Radio、Checkbox 等组件:常见的表单元素。

2. 手动提交表单

antd 表单组件提供了 onFinish 和 onFinishFailed 两个回调函数,分别用于处理表单提交成功和失败的情况。以下是如何手动提交表单的步骤:

2.1 创建表单实例

import { Form } from 'antd';

const form = new Form();

2.2 配置表单项

form.getFieldDecorator('username', {
  rules: [
    { required: true, message: '请输入用户名' },
    { min: 3, max: 16, message: '用户名长度在 3 到 16 个字符' },
  ],
});

2.3 渲染表单元素

<form
  ref="form"
  {...form.getFieldsValue()}
  onsubmit={e => e.preventDefault()}
>
  <FormItem label="用户名">
    {form.getFieldDecorator('username', {
      rules: [
        { required: true, message: '请输入用户名' },
        { min: 3, max: 16, message: '用户名长度在 3 到 16 个字符' },
      ],
    })(<Input placeholder="请输入用户名" />)}
  </FormItem>
  <button type="submit">提交</button>
</form>

2.4 手动提交表单

const submitForm = () => {
  form.validateFields((err, values) => {
    if (!err) {
      console.log('Received values of form: ', values);
      // 在这里进行数据提交操作
    }
  });
};

2.5 触发提交

// 通过按钮点击或其他方式触发提交
<button type="button" onclick={submitForm}>手动提交</button>

3. 与前端框架融合

在实际开发中,我们通常会使用 React、Vue 或 Angular 等前端框架。以下是如何将手动提交表单与 React、Vue 和 Angular 框架融合的示例:

3.1 React

在 React 中,我们可以使用 useRef 钩子获取到表单实例,并在需要时调用 submitForm 函数。

import React, { useRef } from 'react';
import { Form, Input } from 'antd';

const MyForm = () => {
  const formRef = useRef();

  const submitForm = () => {
    formRef.current.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
        // 在这里进行数据提交操作
      }
    });
  };

  return (
    <Form ref={formRef}>
      <Form.Item name="username" rules={[{ required: true, message: '请输入用户名' }]}>
        <Input placeholder="请输入用户名" />
      </Form.Item>
      <button type="button" onclick={submitForm}>手动提交</button>
    </Form>
  );
};

3.2 Vue

在 Vue 中,我们可以使用 this.$refs 获取到表单实例,并在需要时调用 submitForm 方法。

<template>
  <form ref="form">
    <form-item name="username" rules={[{ required: true, message: '请输入用户名' }]}>
      <input placeholder="请输入用户名" />
    </form-item>
    <button type="button" @click="submitForm">手动提交</button>
  </form>
</template>

<script>
import { Form, FormItem, Input } from 'antd';

export default {
  components: {
    Form,
    FormItem,
    Input,
  },
  methods: {
    submitForm() {
      this.$refs.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values);
          // 在这里进行数据提交操作
        }
      });
    },
  },
};
</script>

3.3 Angular

在 Angular 中,我们可以使用 NgForm 和 NgModel 指令获取到表单实例,并在需要时调用 submitForm 方法。

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-my-form',
  templateUrl: './my-form.component.html',
  styleUrls: ['./my-form.component.css'],
})
export class MyFormComponent {
  form: FormGroup;

  constructor(private fb: FormBuilder) {
    this.form = this.fb.group({
      username: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(16)]],
    });
  }

  submitForm() {
    if (this.form.valid) {
      console.log('Received values of form: ', this.form.value);
      // 在这里进行数据提交操作
    }
  }
}

4. 总结

通过本文的介绍,相信你已经掌握了使用 antd 表单手动提交的技巧,并能够将其与前端框架完美融合。在实际开发中,灵活运用这些技巧,可以帮助你更好地解决业务需求。

同件

同组件记录

Same Component