Antd form remote submit
Asked Answered
G

5

6

I want to trigger the submit button outside the antd form

Here is my code

<Form
          {...layout}
          name="basic"
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
        >

     <Form.Item
        label="Username"
        name="username"
        rules={[
          {
            required: true,
            message: 'Please input your username!',
          },
        ]}
      >
        <Input />
      </Form.Item>
    
    <Form.Item {...tailLayout}>
           // Submit button here works well
          </Form.Item>
</Form>

I want this outside form tag, please suggest

 <Button type="primary" htmlType="submit">
     Submit
 </Button>
Gawky answered 12/5, 2020 at 7:15 Comment(0)
T
13

You can try this way:

 const [form] = Form.useForm();

  const handleFinish = (values) => {
    console.log("values: ", values);
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <MyForm form={form} onFinish={handleFinish} />
      <Button onClick={() => form.submit()}>Submit</Button>
    </div>
  );
export const MyForm = ({ form, onFinish }) => {
  return (
    <Form
      form={form}
      onFinish={onFinish}
      initialValues={{ username: "John Doe" }}
    >
      <Form.Item name="username">
        <Input />
      </Form.Item>
    </Form>
  );
};

Edit strange-currying-ttkv1

Tripartition answered 20/11, 2020 at 14:36 Comment(0)
K
4

If the form and the button are not in the same component then it is hard to create same reference for the form in 2 different place. But I found an other easy way:

<Form id="myForm">...</>
...
<Button form="myForm" key="submit" htmlType="submit">
  Submit
</Button>

This is the answer from this issue

Klemm answered 9/2, 2021 at 8:10 Comment(1)
this works only for an uncontrolled form, the answer above is the correct one :)Rosenstein
U
1

write a custom function like this:

function handleSubmit(){
  form.validateFields().then((values) => {
    // do something with values
  })
}

and call it from Button onClick. so you don't need Form onFinish props.

Unbiased answered 12/5, 2020 at 9:52 Comment(2)
how can I create reference for the formGawky
in antd v4 use this: const [form] = Form.useForm();Unbiased
C
0

sorry i was searching and see this old question.

as in the comment you should use const [form] = Form.useForm();

that's a lot of function you can use one of them is submit.

form.submit();

you can refer to document and search for useform:

https://ant.design/components/form/

Cohla answered 7/9, 2020 at 7:43 Comment(0)
G
0

Fortunately antd Button component has great support for fixing this issue

Component 1
<Form name="form-name">
...
</Form>

component 2

<Button htmlType="submit" form="form-name">Submit</Submit>
<Button htmlType="reset" form="form-name">Reset</Submit>

We can use the id or name which is provided in antd form and use its button anywhere or outside of the form in the project

Girl answered 2/10, 2024 at 5:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.