Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested form submits container form #158

Open
rachelleahr opened this issue Feb 12, 2019 · 10 comments
Open

Nested form submits container form #158

rachelleahr opened this issue Feb 12, 2019 · 10 comments

Comments

@rachelleahr
Copy link

I have a form that has a fieldArray in it - that field array uses a form inside with a submit button. Right now whenever the nested form gets submitted it also calls the container's onSubmit. Is that expected behavior or am I doing something wrong?

@jquense
Copy link
Owner

jquense commented Feb 12, 2019

can you post some trimmed down code demonstrating the issue? thanks!

@rachelleahr
Copy link
Author

rachelleahr commented Feb 12, 2019

Something like this

const contactsSchema = Yup.object().shape({
  contacts: Yup.array()
    .of(Yup.object())
    .test('containsPrimaryContact', 'Primary contact is required', values =>
      values.some(v => v.isPrimaryContact)
    )
});

...

this.state = {
      currentlyEditing: 0,
      contacts: [{ ...defaultContact }],
      errors: {}
};

....

<Form
            as="div"
            schema={contactsSchema}
            value={{ contacts }}
            onChange={() => {}}
            onSubmit={values => {
              console.log('form submitted');
              onSave(values);
            }}
          >
            <Form.FieldArray name="contacts" events="blur">
              {({ value, arrayHelpers }) => (
                <div>
                  {value.map((c, i) => {
                    if (i === currentlyEditing) {
                      return (
                        <Form
                          onSubmit={values => {
                            this.onSave(values, i);
                            this.setState({
                              submitted: true,
                              currentlyEditing: -1
                            });
                          }}
                          contact={c}
                          index={i}
                          onCancel={() => arrayHelpers.remove(c)}
                          key={i.toString()}
                          errors={errors}
                          setTouched={() => this.setState({ touched: true })}
                          onError={e => this.setState({ errors: e })}
                        >
                       {form fields}
                       <Form.Submit
                             as={Button}
                             isPrimary
                             width={154}
                             height={54.6}
                             label="Save"
                        />
                      </Form>
                      );
                    }
                    return (
                      <ClosedContact
                        isOpen
                        contact={c}
                        editContact={() => this.setState({ currentlyEditing: i })}
                        index={i}
                      />
                    );
                  })}
                  {currentlyEditing >= 0 ? null : (
                    <PlusIcon onClick={this.addContact} />
                  )}
                </div>
              )}
            </Form.FieldArray>
            <Form.Message  for="contacts" />
            <div >
              <Form.Submit
                as={Button}
                isPrimary
                width={154}
                height={54.6}
                label="Finish"
              />
            </div>
          </Form>

@rachelleahr
Copy link
Author

I took a look at some of the source code and I can't find where the NestedForm component is exported - I was using the regular form, maybe that's the problem?

@jquense
Copy link
Owner

jquense commented Feb 13, 2019

so What i think may be happening is the native submit event is bubbling up in each form, but it's a bit hard to know without a test case. I would first make sure you are actually nesting html <form> elements, so the inner Form should set the the as prop to something like a div.

@rachelleahr
Copy link
Author

Thanks for the quick response - that seems to have done the trick! 👍
switched all forms to use divs instead

@thejohnfreeman
Copy link

Just now discovering this library. Wish I knew about it before embarking on my own. I would love to join forces.

@jquense, I haven't dug into your code yet, but if you're doing it like you do in topeka (and like I am), then you're providing a context in the Form component. The way I solved this problem is to add that context to the Form itself, use null as the default, and then check in the Form.render method whether the context is non-null and conditionally wrap the children in a <form> HTML element.

@rachelleahr
Copy link
Author

@thejohnfreeman I'm confused as to how this solves my problem? or was that just a general suggestion?

@thejohnfreeman
Copy link

Sorry, @racheldelman, it's a suggestion to @jquense for how to fix this problem in the library, not for you as a user.

@rachelleahr
Copy link
Author

oh ok thanks! as I said before the NestdForm doesn't even seem to be exported anywhere so I assume this is more to fix there as well

@jquense
Copy link
Owner

jquense commented Feb 13, 2019

thats a good idea @thejohnfreeman for setting the default inner element 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants