简体   繁体   English

AntD React 表单提交不包括所有字段

[英]AntD React Form Submit not including all fields

I have a multi-step form built with the antd React library.我有一个使用 antd React 库构建的多步骤表单。 I conditionally render the fields based on which stage I'm in, but all fields are under the same Form .我根据我所在的阶段有条件地呈现字段,但所有字段都在同一个Form

When I log the values passed to the onFinish handler, values doesn't include the fields from the first step of my form.当我登录的values传递给onFinish处理器, values不包括我的表格的第一个步骤中的字段。 values["name"] doesn't exist, but if I check form.getFieldValue("name") the value I expect is present on the form . values["name"]不存在,但是如果我检查form.getFieldValue("name")我期望的值出现在form

import React, { Fragment } from 'react';
import { Form, Input, DatePicker, Button, Card, Checkbox, InputNumber } from 'antd';

... boilerplate

const [ form ] = Form.useForm();
const onFinish = values => {
     // includes "location" but not "name"
     console.log(values);
}

let formContent;
    switch (stage) {
        case 1:
            formContent = (
                <Fragment>
                    <Form.Item name="name" label="Name">
                        <Input />
                    </Form.Item>  
                </Fragment>
            )
            break;
        case 2:
            formContent = (
                <Fragment>
                    <Form.Item name="Location" label="Location">
                        <InputNumber />
                    </Form.Item>
                        <Button
                            className="next-button"
                            type="primary"
                            htmlType="submit"
                        >
                            Create
                        </Button>
                    </Form.Item>
                </Fragment>
            )
            break;
        default:
            formContent = null;
    }
    return (
        <div className="CreateTournamentForm">
            <Card
                title={title}
            >
                <Form
                    name="create-form"
                    labelCol={{ span: 10 }}
                    wrapperCol={{ span: 40 }}
                    layout="horizontal"
                    size="large"
                    form={form}
                    onFinish={onFinish}
                    initialValues={defaultValues}
                >
                    { formContent }
                </Form>
            </Card>
        </div>
    )

You don't show the logic behind declaring the stage value.您没有显示声明stage值背后的逻辑。 But from what you're describing, stage 2 overrides the first form, assigning a new one, which is like this:但是根据您的描述,第 2 阶段覆盖了第一种形式,分配了一个新形式,如下所示:

<Fragment>
  <Form.Item name="Location" label="Location">
    <InputNumber />
  </Form.Item>
  <Button className="next-button" type="primary" htmlType="submit">
    Create
  </Button>
</Fragment>;

This deletes the old values (name).这将删除旧值(名称)。

If your form consists in various stages, I'd create one for each stage and save the aggregates to the state.如果您的表单包含多个阶段,我会为每个阶段创建一个并将聚合保存到状态。 Then fetch whatever you want with the accumulation of the user inputs.然后通过用户输入的积累来获取您想要的任何内容。

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

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