How to submit form component in modal dialogue using antd react component library












7















In my component's render method I have antd Modal component as a parent and antd Form component as a child:



    render() {
const myForm = Form.create()(AddNewItemForm);
...
return (
...
<Modal
title="Create new item"
visible={this.state.visible}
onOk={this.handleOk}
onCancel={this.handleCancel}
wrapClassName="vertical-center-modal"
okText="Save new item"
width="600"
>
<myForm />
</Modal>
...


How can I submit my form by clicking the Modals Save button?










share|improve this question





























    7















    In my component's render method I have antd Modal component as a parent and antd Form component as a child:



        render() {
    const myForm = Form.create()(AddNewItemForm);
    ...
    return (
    ...
    <Modal
    title="Create new item"
    visible={this.state.visible}
    onOk={this.handleOk}
    onCancel={this.handleCancel}
    wrapClassName="vertical-center-modal"
    okText="Save new item"
    width="600"
    >
    <myForm />
    </Modal>
    ...


    How can I submit my form by clicking the Modals Save button?










    share|improve this question



























      7












      7








      7


      2






      In my component's render method I have antd Modal component as a parent and antd Form component as a child:



          render() {
      const myForm = Form.create()(AddNewItemForm);
      ...
      return (
      ...
      <Modal
      title="Create new item"
      visible={this.state.visible}
      onOk={this.handleOk}
      onCancel={this.handleCancel}
      wrapClassName="vertical-center-modal"
      okText="Save new item"
      width="600"
      >
      <myForm />
      </Modal>
      ...


      How can I submit my form by clicking the Modals Save button?










      share|improve this question
















      In my component's render method I have antd Modal component as a parent and antd Form component as a child:



          render() {
      const myForm = Form.create()(AddNewItemForm);
      ...
      return (
      ...
      <Modal
      title="Create new item"
      visible={this.state.visible}
      onOk={this.handleOk}
      onCancel={this.handleCancel}
      wrapClassName="vertical-center-modal"
      okText="Save new item"
      width="600"
      >
      <myForm />
      </Modal>
      ...


      How can I submit my form by clicking the Modals Save button?







      reactjs antd






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 26 '16 at 10:03









      Sergio

      24.3k862106




      24.3k862106










      asked Dec 19 '16 at 11:31









      vladimirpvladimirp

      4333816




      4333816
























          4 Answers
          4






          active

          oldest

          votes


















          5














          My solution was to wrap modal dialogue and form components in a new wrapper parent component in which I validate the child form component in handleCreate method. I have used the ref attribute to reference the myForm child component inside the FormOnModalWrapper component. I am passing the parent handlers via props from the wrapper parent component to myForm component instance.



          class FormOnModalWrapper extends React.Component {
          ...
          constructor(props) {
          this.state =
          {
          visible: false
          ....
          }

          ...
          showModal = () => {
          this.setState({
          visible: true,
          });
          }

          handleCreate = () => {
          const form = this.form;
          form.validateFields((err, values) => {
          if (err) {
          return;
          }
          console.log('Received values of form: ', values);
          form.resetFields();
          this.setState({ visible: false });
          });
          }

          saveFormRef = (form) => {
          this.form = form;
          }

          render() {
          ...
          const myForm= Form.create()(CrateNewItemFormOnModal);
          ...
          return (
          <div>
          <Button onClick={this.showModal}>Add</Button>
          <myForm
          visible={this.state.visible}
          onCancel={this.handleCancel}
          onCreate={this.handleCreate}
          ref={this.saveFormRef}
          />
          </div>
          );
          }


          In CrateNewItemFormOnModal component class I have a modal dialogue component as a parent and form component as a child:



          export default class AddNewItemForm extends React.Component {

          render() {
          ...
          const { visible, onCancel, onCreate, form } = this.props;
          ...
          return (
          <Modal
          title="Create new item"
          visible={visible}
          onOk={onCreate}
          onCancel={onCancel}
          okText="Create"
          >
          <Form>
          ...
          </Form>
          </Modal>
          );
          }





          share|improve this answer

































            3














            You can study official example: https://ant.design/components/form/#components-form-demo-form-in-modal






            share|improve this answer



















            • 1





              A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

              – zuazo
              Dec 27 '16 at 2:07



















            1














            There is a new solution that looks much cleaner:



            <Form id="myForm">


            ...



            <Modal
            ...
            footer={[
            <Button form="myForm" key="submit" htmlType="submit">
            Submit
            </Button>
            ]}
            >
            <CustomForm />
            </Modal>


            This works because of the Button's form attribute. Browser support



            Original solution's author: https://github.com/ant-design/ant-design/issues/9380






            share|improve this answer































              0














              My solution was to disable the modal's footer and create my own submit button:



              <Modal footer={null}>
              <Form onSubmit={this.customSubmit}>
              ...
              <FormItem>
              <Button type="primary" htmlType="submit">Submit</Button>
              </FormItem>
              </Form>
              </Modal>


              No need to wrap the modal with this solution.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41221633%2fhow-to-submit-form-component-in-modal-dialogue-using-antd-react-component-librar%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                5














                My solution was to wrap modal dialogue and form components in a new wrapper parent component in which I validate the child form component in handleCreate method. I have used the ref attribute to reference the myForm child component inside the FormOnModalWrapper component. I am passing the parent handlers via props from the wrapper parent component to myForm component instance.



                class FormOnModalWrapper extends React.Component {
                ...
                constructor(props) {
                this.state =
                {
                visible: false
                ....
                }

                ...
                showModal = () => {
                this.setState({
                visible: true,
                });
                }

                handleCreate = () => {
                const form = this.form;
                form.validateFields((err, values) => {
                if (err) {
                return;
                }
                console.log('Received values of form: ', values);
                form.resetFields();
                this.setState({ visible: false });
                });
                }

                saveFormRef = (form) => {
                this.form = form;
                }

                render() {
                ...
                const myForm= Form.create()(CrateNewItemFormOnModal);
                ...
                return (
                <div>
                <Button onClick={this.showModal}>Add</Button>
                <myForm
                visible={this.state.visible}
                onCancel={this.handleCancel}
                onCreate={this.handleCreate}
                ref={this.saveFormRef}
                />
                </div>
                );
                }


                In CrateNewItemFormOnModal component class I have a modal dialogue component as a parent and form component as a child:



                export default class AddNewItemForm extends React.Component {

                render() {
                ...
                const { visible, onCancel, onCreate, form } = this.props;
                ...
                return (
                <Modal
                title="Create new item"
                visible={visible}
                onOk={onCreate}
                onCancel={onCancel}
                okText="Create"
                >
                <Form>
                ...
                </Form>
                </Modal>
                );
                }





                share|improve this answer






























                  5














                  My solution was to wrap modal dialogue and form components in a new wrapper parent component in which I validate the child form component in handleCreate method. I have used the ref attribute to reference the myForm child component inside the FormOnModalWrapper component. I am passing the parent handlers via props from the wrapper parent component to myForm component instance.



                  class FormOnModalWrapper extends React.Component {
                  ...
                  constructor(props) {
                  this.state =
                  {
                  visible: false
                  ....
                  }

                  ...
                  showModal = () => {
                  this.setState({
                  visible: true,
                  });
                  }

                  handleCreate = () => {
                  const form = this.form;
                  form.validateFields((err, values) => {
                  if (err) {
                  return;
                  }
                  console.log('Received values of form: ', values);
                  form.resetFields();
                  this.setState({ visible: false });
                  });
                  }

                  saveFormRef = (form) => {
                  this.form = form;
                  }

                  render() {
                  ...
                  const myForm= Form.create()(CrateNewItemFormOnModal);
                  ...
                  return (
                  <div>
                  <Button onClick={this.showModal}>Add</Button>
                  <myForm
                  visible={this.state.visible}
                  onCancel={this.handleCancel}
                  onCreate={this.handleCreate}
                  ref={this.saveFormRef}
                  />
                  </div>
                  );
                  }


                  In CrateNewItemFormOnModal component class I have a modal dialogue component as a parent and form component as a child:



                  export default class AddNewItemForm extends React.Component {

                  render() {
                  ...
                  const { visible, onCancel, onCreate, form } = this.props;
                  ...
                  return (
                  <Modal
                  title="Create new item"
                  visible={visible}
                  onOk={onCreate}
                  onCancel={onCancel}
                  okText="Create"
                  >
                  <Form>
                  ...
                  </Form>
                  </Modal>
                  );
                  }





                  share|improve this answer




























                    5












                    5








                    5







                    My solution was to wrap modal dialogue and form components in a new wrapper parent component in which I validate the child form component in handleCreate method. I have used the ref attribute to reference the myForm child component inside the FormOnModalWrapper component. I am passing the parent handlers via props from the wrapper parent component to myForm component instance.



                    class FormOnModalWrapper extends React.Component {
                    ...
                    constructor(props) {
                    this.state =
                    {
                    visible: false
                    ....
                    }

                    ...
                    showModal = () => {
                    this.setState({
                    visible: true,
                    });
                    }

                    handleCreate = () => {
                    const form = this.form;
                    form.validateFields((err, values) => {
                    if (err) {
                    return;
                    }
                    console.log('Received values of form: ', values);
                    form.resetFields();
                    this.setState({ visible: false });
                    });
                    }

                    saveFormRef = (form) => {
                    this.form = form;
                    }

                    render() {
                    ...
                    const myForm= Form.create()(CrateNewItemFormOnModal);
                    ...
                    return (
                    <div>
                    <Button onClick={this.showModal}>Add</Button>
                    <myForm
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    onCreate={this.handleCreate}
                    ref={this.saveFormRef}
                    />
                    </div>
                    );
                    }


                    In CrateNewItemFormOnModal component class I have a modal dialogue component as a parent and form component as a child:



                    export default class AddNewItemForm extends React.Component {

                    render() {
                    ...
                    const { visible, onCancel, onCreate, form } = this.props;
                    ...
                    return (
                    <Modal
                    title="Create new item"
                    visible={visible}
                    onOk={onCreate}
                    onCancel={onCancel}
                    okText="Create"
                    >
                    <Form>
                    ...
                    </Form>
                    </Modal>
                    );
                    }





                    share|improve this answer















                    My solution was to wrap modal dialogue and form components in a new wrapper parent component in which I validate the child form component in handleCreate method. I have used the ref attribute to reference the myForm child component inside the FormOnModalWrapper component. I am passing the parent handlers via props from the wrapper parent component to myForm component instance.



                    class FormOnModalWrapper extends React.Component {
                    ...
                    constructor(props) {
                    this.state =
                    {
                    visible: false
                    ....
                    }

                    ...
                    showModal = () => {
                    this.setState({
                    visible: true,
                    });
                    }

                    handleCreate = () => {
                    const form = this.form;
                    form.validateFields((err, values) => {
                    if (err) {
                    return;
                    }
                    console.log('Received values of form: ', values);
                    form.resetFields();
                    this.setState({ visible: false });
                    });
                    }

                    saveFormRef = (form) => {
                    this.form = form;
                    }

                    render() {
                    ...
                    const myForm= Form.create()(CrateNewItemFormOnModal);
                    ...
                    return (
                    <div>
                    <Button onClick={this.showModal}>Add</Button>
                    <myForm
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    onCreate={this.handleCreate}
                    ref={this.saveFormRef}
                    />
                    </div>
                    );
                    }


                    In CrateNewItemFormOnModal component class I have a modal dialogue component as a parent and form component as a child:



                    export default class AddNewItemForm extends React.Component {

                    render() {
                    ...
                    const { visible, onCancel, onCreate, form } = this.props;
                    ...
                    return (
                    <Modal
                    title="Create new item"
                    visible={visible}
                    onOk={onCreate}
                    onCancel={onCancel}
                    okText="Create"
                    >
                    <Form>
                    ...
                    </Form>
                    </Modal>
                    );
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 23 '16 at 13:44

























                    answered Dec 23 '16 at 13:36









                    vladimirpvladimirp

                    4333816




                    4333816

























                        3














                        You can study official example: https://ant.design/components/form/#components-form-demo-form-in-modal






                        share|improve this answer



















                        • 1





                          A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

                          – zuazo
                          Dec 27 '16 at 2:07
















                        3














                        You can study official example: https://ant.design/components/form/#components-form-demo-form-in-modal






                        share|improve this answer



















                        • 1





                          A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

                          – zuazo
                          Dec 27 '16 at 2:07














                        3












                        3








                        3







                        You can study official example: https://ant.design/components/form/#components-form-demo-form-in-modal






                        share|improve this answer













                        You can study official example: https://ant.design/components/form/#components-form-demo-form-in-modal







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 27 '16 at 1:52









                        benjycuibenjycui

                        28712




                        28712








                        • 1





                          A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

                          – zuazo
                          Dec 27 '16 at 2:07














                        • 1





                          A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

                          – zuazo
                          Dec 27 '16 at 2:07








                        1




                        1





                        A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

                        – zuazo
                        Dec 27 '16 at 2:07





                        A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

                        – zuazo
                        Dec 27 '16 at 2:07











                        1














                        There is a new solution that looks much cleaner:



                        <Form id="myForm">


                        ...



                        <Modal
                        ...
                        footer={[
                        <Button form="myForm" key="submit" htmlType="submit">
                        Submit
                        </Button>
                        ]}
                        >
                        <CustomForm />
                        </Modal>


                        This works because of the Button's form attribute. Browser support



                        Original solution's author: https://github.com/ant-design/ant-design/issues/9380






                        share|improve this answer




























                          1














                          There is a new solution that looks much cleaner:



                          <Form id="myForm">


                          ...



                          <Modal
                          ...
                          footer={[
                          <Button form="myForm" key="submit" htmlType="submit">
                          Submit
                          </Button>
                          ]}
                          >
                          <CustomForm />
                          </Modal>


                          This works because of the Button's form attribute. Browser support



                          Original solution's author: https://github.com/ant-design/ant-design/issues/9380






                          share|improve this answer


























                            1












                            1








                            1







                            There is a new solution that looks much cleaner:



                            <Form id="myForm">


                            ...



                            <Modal
                            ...
                            footer={[
                            <Button form="myForm" key="submit" htmlType="submit">
                            Submit
                            </Button>
                            ]}
                            >
                            <CustomForm />
                            </Modal>


                            This works because of the Button's form attribute. Browser support



                            Original solution's author: https://github.com/ant-design/ant-design/issues/9380






                            share|improve this answer













                            There is a new solution that looks much cleaner:



                            <Form id="myForm">


                            ...



                            <Modal
                            ...
                            footer={[
                            <Button form="myForm" key="submit" htmlType="submit">
                            Submit
                            </Button>
                            ]}
                            >
                            <CustomForm />
                            </Modal>


                            This works because of the Button's form attribute. Browser support



                            Original solution's author: https://github.com/ant-design/ant-design/issues/9380







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 16:43









                            sensorsensor

                            1,33611527




                            1,33611527























                                0














                                My solution was to disable the modal's footer and create my own submit button:



                                <Modal footer={null}>
                                <Form onSubmit={this.customSubmit}>
                                ...
                                <FormItem>
                                <Button type="primary" htmlType="submit">Submit</Button>
                                </FormItem>
                                </Form>
                                </Modal>


                                No need to wrap the modal with this solution.






                                share|improve this answer




























                                  0














                                  My solution was to disable the modal's footer and create my own submit button:



                                  <Modal footer={null}>
                                  <Form onSubmit={this.customSubmit}>
                                  ...
                                  <FormItem>
                                  <Button type="primary" htmlType="submit">Submit</Button>
                                  </FormItem>
                                  </Form>
                                  </Modal>


                                  No need to wrap the modal with this solution.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    My solution was to disable the modal's footer and create my own submit button:



                                    <Modal footer={null}>
                                    <Form onSubmit={this.customSubmit}>
                                    ...
                                    <FormItem>
                                    <Button type="primary" htmlType="submit">Submit</Button>
                                    </FormItem>
                                    </Form>
                                    </Modal>


                                    No need to wrap the modal with this solution.






                                    share|improve this answer













                                    My solution was to disable the modal's footer and create my own submit button:



                                    <Modal footer={null}>
                                    <Form onSubmit={this.customSubmit}>
                                    ...
                                    <FormItem>
                                    <Button type="primary" htmlType="submit">Submit</Button>
                                    </FormItem>
                                    </Form>
                                    </Modal>


                                    No need to wrap the modal with this solution.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 18 '18 at 19:24









                                    JacopoStanchiJacopoStanchi

                                    43014




                                    43014






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41221633%2fhow-to-submit-form-component-in-modal-dialogue-using-antd-react-component-librar%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        MongoDB - Not Authorized To Execute Command

                                        How to fix TextFormField cause rebuild widget in Flutter

                                        Npm cannot find a required file even through it is in the searched directory