jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.98k stars 2.79k forks source link

Value for a select with dynamic options doesn't seem to submit? #2621

Open jhackett1 opened 4 years ago

jhackett1 commented 4 years ago

Current Behavior

Summary: I have a form with a select input that has dynamic options. Formik doesn't seem to pick up the correct values.

I'm building a Formik form with two parts:

In the first part, the user provides an array of strings.

In the second, the user uses a select to choose one option, where the options are dynamically populated from the values given in the first half, like this

<Field as="select" name="favoruite">
    {values.podcasters.map((name, i) =>
        <option value={name} key={i}>{name}</option>
    )}
</Field>

Since the user provides the values for this select as part of the form, I've set formik's initial value to an empty string.The select should default to the first value in the array of options.

Looking at the form on the screen, everything behaves and functions fine — I can see the select input "fill out" as I type values into the first part of the form, but when I inspect the array of values on submission it only contains the initial empty string, and my "myKey can't be blank" validation is firing on it.

I think I either need to somehow force formik to grab the value from the select input, either when i submit the form, or as the earlier inputs where the user is supplying the options for it are changed?

Code sandbox

Expected behavior

Formik's value object should correctly pick up the up-to-date value of the select, at minimum when i submit the form.

If this isn't a bug, some docs on how to handle the case of a select with dynamic options would be appreciated.

Your environment

{
"formik": "^2.1.4",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.1",
}

Firefox, MacOS

viniciusdacal commented 4 years ago

The link for the sandbox seems wrong (Request failed with status code 404).

But something I noticed, it seems you have a typo in your field's name name="favoruite" -> name="favourite". Any chance the bug is related to that?

If it's not that, please update the link for the sandbox. Best.