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

Multiple Forms on the same page #1367

Closed
tylergaugler16 opened this issue Mar 6, 2019 · 9 comments
Closed

Multiple Forms on the same page #1367

tylergaugler16 opened this issue Mar 6, 2019 · 9 comments

Comments

@tylergaugler16
Copy link

How to prevent strange behavior when having two formik components on the same page❓

I have a login and signup form on the same page. Both forms work totally fine in isolation. The main structure I have is this:

<SignupAndLoginWrapper>
  <LoginForm />
  <SignupForm />
</SignupAndLoginWrapper>

And this is an example of how one of my forms looks:

<Form
          key="login-form"
          initialValues={{ email: "", password: "", test: "" }}
          onSubmit={(values, { setSubmitting }) => {
            this.handleLogin(values);
            setSubmitting(true);
          }}
        >
          {({ values, errors, touched, handleSubmit, handleChange }) => (
            <form onSubmit={handleSubmit} key="login-form-form" id="login-form">
              <div className="columns is-gapless is-centered">
                <div className="column is-10 ">
                  <Field
                    key="login.email"
                    name="email"
                    label="Email"
                    component={TextInput}
                    validate={isEmail}
                  />
                </div>
              </div>
              <div className="columns is-gapless is-centered">
                <div className="column is-10">
                  <Field
                    key="login.password"
                    name="password"
                    label="Password"
                    component={PasswordInput}
                    validate={required}
                  />
                </div>
              </div>
              <div className="columns is-gapless is-centered">
                <div className="column is-10">
                  <Field
                    key="login.test"
                    name="test"
                    label="Test"
                    component={PasswordInput}
                    validate={required}
                  />
                </div>
              </div>
              <div className="columns is-gapless is-centered">
                <div className="column is-10 ">
                  <button
                    className="button medium-button purple-button is-centered-block"
                    type="submit"
                  >
                    Submit
                  </button>
                </div>
              </div>
            </form>
          )}
        </Form>

What's happening is that my signup form works fine, but for my login form none of the inputs work. And when i use the react inspector, I notice that the signup fields are inside my Login component (when in my actual component they are not).

When i do display: none on my signup form, the login form behaves normal btw.

I've tried using keys on both forms to keep them distinct, but that hasnt worked. Any ideas?

@tylergaugler16
Copy link
Author

Realizing my custom animations were leading the input fields to appear on top of each other and lead to this confusing behavior. Closing issue.

@edwinwong90
Copy link

edwinwong90 commented Jun 5, 2019

@tylergaugler16 rules of form never nest them <form>.

@philals
Copy link

philals commented Dec 1, 2019

Came here from a google.

I had two forms toggled by state on the same page. Giving them different keys solved my weird behavior.

@mateja176
Copy link

@philals Is there any documentation regard the key prop?

@philals
Copy link

philals commented Sep 8, 2020

@mateja176 Hi really sorry I tried looking for the code to remind myself - I even looked at my commits for that day...

I think I was referring to React's standard key prop. https://reactjs.org/docs/lists-and-keys.html

I have a feeling adding this to the Formik component allows it to have two forms on the same page.

If anyone is able to confirm my memory that would be great.

@mateja176
Copy link

@mateja176 Hi really sorry I tried looking for the code to remind myself - I even looked at my commits for that day...

I think I was referring to React's standard key prop. https://reactjs.org/docs/lists-and-keys.html

I have a feeling adding this to the Formik component allows it to have two forms on the same page.

If anyone is able to confirm my memory that would be great.

I'd be inclined to agree if the prop types of Form didn't explicitly contain a key prop. There may be more to it, since the key prop is usually managed by React. Nevertheless, this might be a red herring.

@Cristyander
Copy link

You can submit your information on onClick from your submit button. onClick = {() => console.log (values)}

@dev-bhuptani
Copy link

dev-bhuptani commented Jul 14, 2021

@mateja176 Hi really sorry I tried looking for the code to remind myself - I even looked at my commits for that day...

I think I was referring to React's standard key prop. https://reactjs.org/docs/lists-and-keys.html

I have a feeling adding this to the Formik component allows it to have two forms on the same page.

If anyone is able to confirm my memory that would be great.

Thanks, it helped! I'm able to have two different Formik components on the same page by adding key property. But, I'm curious whether this is a correct approach or not. Can someone fill me on how it works by adding key property?

@pdhankhar61
Copy link

pdhankhar61 commented Feb 25, 2023

The new react hook useId() might help for keys.

https://beta.reactjs.org/reference/react/useId

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

7 participants