单击提交按钮时 Formik 未提交。 (反应和 NextJS)

发布于 2025-01-09 14:45:12 字数 2665 浏览 2 评论 0原文

加载页面时会自动填写表单输入campaignid

我已将 onChangeonBlur 添加到 name 输入中以防止出现默认情况,但是当您单击提交按钮时,不会执行任何操作。

我正在使用第三方脚本来获取 URL 参数并将其插入到 campaignid 值中。

import * as React from "react";
import Script from "next/script";
import { Field, Form, Formik } from "formik";   

import {
  Button,
  Flex,
  FormControl,
  FormErrorMessage,
  FormLabel,
  Input,
} from "@chakra-ui/react";


export default function FormikForm() {
  function validateName(value) {
    let error;
    if (!value) {
      error = "Type a valid name.";
    }
    return error;
  }

  return (
    <>
      <Script src="https://cdn.jsdelivr.net/gh/gkogan/sup-save-url-parameters/sup.min.js" />

      <Formik
        initialValues={{
          name: "",
        }}
        onSubmit={(values) => {
          setTimeout(() => {
            fetch(`https://hooks.zapier.com/hooks/catch/3660927/bte5w75/`, {
              method: "POST",
              body: JSON.stringify(values, null, 2),
            }),
              3000;
          });
        }}
      >
        {(props) => (
          <Flex>
            <Form id="hero">
              <Field name="name" validate={validateName}>
                {({ field, form }) => (
                  <FormControl
                    isInvalid={form.errors.name && form.touched.name}
                  >
                    <FormLabel htmlFor="name">Nome</FormLabel>
                    <Input
                      {...field}
                      id="name"
                      onChange={(e) => {
                        e.preventDefault();
                      }}
                      onBlur={(e) => {
                        e.preventDefault();
                      }}
                    />
                    <FormErrorMessage>{form.errors.name}</FormErrorMessage>
                  </FormControl>
                )}
              </Field>

              <Field name="campaignid">
                {({ field }) => (
                  <FormControl isReadOnly>
                    <Input {...field} type="hidden" id="campaignid" />
                  </FormControl>
                )}
              </Field>

              <Button
                id="submited"
                isLoading={props.isSubmitting}
                type="submit"
              >
                Submit
              </Button>
            </Form>
          </Flex>
        )}
      </Formik>
    </>
  );
}

The form input campaignid is automatically filled in when the page is loaded.

I've added onChange and onBlur to name input to prevent default, however when you click on the submit button, no action is taken.

I'm using a third party script to get URL parameters and insert them into campaignid value.

import * as React from "react";
import Script from "next/script";
import { Field, Form, Formik } from "formik";   

import {
  Button,
  Flex,
  FormControl,
  FormErrorMessage,
  FormLabel,
  Input,
} from "@chakra-ui/react";


export default function FormikForm() {
  function validateName(value) {
    let error;
    if (!value) {
      error = "Type a valid name.";
    }
    return error;
  }

  return (
    <>
      <Script src="https://cdn.jsdelivr.net/gh/gkogan/sup-save-url-parameters/sup.min.js" />

      <Formik
        initialValues={{
          name: "",
        }}
        onSubmit={(values) => {
          setTimeout(() => {
            fetch(`https://hooks.zapier.com/hooks/catch/3660927/bte5w75/`, {
              method: "POST",
              body: JSON.stringify(values, null, 2),
            }),
              3000;
          });
        }}
      >
        {(props) => (
          <Flex>
            <Form id="hero">
              <Field name="name" validate={validateName}>
                {({ field, form }) => (
                  <FormControl
                    isInvalid={form.errors.name && form.touched.name}
                  >
                    <FormLabel htmlFor="name">Nome</FormLabel>
                    <Input
                      {...field}
                      id="name"
                      onChange={(e) => {
                        e.preventDefault();
                      }}
                      onBlur={(e) => {
                        e.preventDefault();
                      }}
                    />
                    <FormErrorMessage>{form.errors.name}</FormErrorMessage>
                  </FormControl>
                )}
              </Field>

              <Field name="campaignid">
                {({ field }) => (
                  <FormControl isReadOnly>
                    <Input {...field} type="hidden" id="campaignid" />
                  </FormControl>
                )}
              </Field>

              <Button
                id="submited"
                isLoading={props.isSubmitting}
                type="submit"
              >
                Submit
              </Button>
            </Form>
          </Flex>
        )}
      </Formik>
    </>
  );
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文