提交后,从最终JSON中删除隐藏的字段

发布于 2025-02-10 00:43:38 字数 2625 浏览 1 评论 0原文

我使用Formik构建了一个简单的表单。我的情况是,在我选择Radio Buttun“否”之后,一个领域就被隐藏了。 但是,如果我输入该字段的价值,然后选择“否”隐藏它,然后提交表格,我将在最终的JSON中获得隐藏字段的值。 我的期望是,当字段未显示到屏幕时,将其从最终的JSON中删除。

我的表格:

import React from "react";
import { Formik } from "formik";
import { Form } from "react-bootstrap";

import { Radio } from "antd";

const CustomFormik = () => {
  return (
    <>
      <Formik
        initialValues={{
          input1: null,
          radio: null,
          input2: null
        }}
        onSubmit={(values) => {
          console.log(values);
        }}
      >
        {(formik) => (
          <>
            <Form
              onSubmit={formik.handleSubmit}
              className="custom-formik-container"
            >
              <Form.Group>
                <span>input1: </span>
                <Form.Control
                  id={"input1"}
                  name={"input1"}
                  {...formik.getFieldProps("input1")}
                />
              </Form.Group>

              {formik.getFieldProps("radio").value !== 2 && (
                <Form.Group>
                  <span>input2: </span>
                  <Form.Control
                    id={"input2"}
                    name={"input2"}
                    {...formik.getFieldProps("input2")}
                  />
                </Form.Group>
              )}

              <Form.Group>
                <span>radio: </span>
                <Radio.Group {...formik.getFieldProps("radio")}>
                  <Radio value={1}>yes</Radio>
                  <Radio value={2}>no</Radio>
                </Radio.Group>
              </Form.Group>
            </Form>

            <button
              style={{
                width: 200,
                height: 30,
                background: "#113776",
                color: "#fff"
              }}
              onClick={() => formik.handleSubmit()}
            >
              S U B M I T
            </button>
          </>
        )}
      </Formik>
    </>
  );
};
export default CustomFormik;

例如,输入: “ ABC”到第一个输入。 “ EFG”到第二个输入。 “否”收音机按钮。

Console.log中的最终JSON将是:

{input1: "abc",input2: "efg", radio: 2}

但是我期望为:

{input1: "abc", radio: 2}

codesandbox

谢谢你们!

I build a simple form using formik. I have a situation that one field is hidden after I choose in radio buttun 'no'.
But if I enter value to this field and then choose 'no' to hide it, and submit the form I will get in the final json also the value of the hidden field.
My expectation is when field isn't show to the screen, remove it from the final json.

My form:

import React from "react";
import { Formik } from "formik";
import { Form } from "react-bootstrap";

import { Radio } from "antd";

const CustomFormik = () => {
  return (
    <>
      <Formik
        initialValues={{
          input1: null,
          radio: null,
          input2: null
        }}
        onSubmit={(values) => {
          console.log(values);
        }}
      >
        {(formik) => (
          <>
            <Form
              onSubmit={formik.handleSubmit}
              className="custom-formik-container"
            >
              <Form.Group>
                <span>input1: </span>
                <Form.Control
                  id={"input1"}
                  name={"input1"}
                  {...formik.getFieldProps("input1")}
                />
              </Form.Group>

              {formik.getFieldProps("radio").value !== 2 && (
                <Form.Group>
                  <span>input2: </span>
                  <Form.Control
                    id={"input2"}
                    name={"input2"}
                    {...formik.getFieldProps("input2")}
                  />
                </Form.Group>
              )}

              <Form.Group>
                <span>radio: </span>
                <Radio.Group {...formik.getFieldProps("radio")}>
                  <Radio value={1}>yes</Radio>
                  <Radio value={2}>no</Radio>
                </Radio.Group>
              </Form.Group>
            </Form>

            <button
              style={{
                width: 200,
                height: 30,
                background: "#113776",
                color: "#fff"
              }}
              onClick={() => formik.handleSubmit()}
            >
              S U B M I T
            </button>
          </>
        )}
      </Formik>
    </>
  );
};
export default CustomFormik;

For example enter:
'abc' to the first input.
'efg' to the second input.
'no' to the radio button.

The final json in console.log will be:

{input1: "abc",input2: "efg", radio: 2}

But my expectation in to be:

{input1: "abc", radio: 2}

codesandbox

Thank you guys!

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

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

发布评论

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

评论(2

俯瞰星空 2025-02-17 00:43:38

提交属性之前删除属性

我不知道这是否适合您的需求,但是我在项目中使用redux-form也有类似的情况,而我所做的就是在这样

    onSubmit={(values) => {
      if (values.radio === 2) {
        let clonedValues = { ...values };
        delete clonedValues.input2;

        console.log(clonedValues);
      } else {
        console.log(values);
      }
    }}

I don't know if this fits your needs but I had a similar case in my project using redux-form and what i did was to delete the property before submiting it

sth like this

    onSubmit={(values) => {
      if (values.radio === 2) {
        let clonedValues = { ...values };
        delete clonedValues.input2;

        console.log(clonedValues);
      } else {
        console.log(values);
      }
    }}
情未る 2025-02-17 00:43:38

这将完成这项工作。

        onSubmit={(values) => {
      if(values.radio === 2){
        delete values['input2'];
      }
      console.log(values);
    }}

this will do the job.

        onSubmit={(values) => {
      if(values.radio === 2){
        delete values['input2'];
      }
      console.log(values);
    }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文