jaredpalmer / formik

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

setFieldValue deleting the value #2098

Closed zsobral closed 4 years ago

zsobral commented 4 years ago

🐛 Bug report

Current Behavior

when use setFieldValue passing a empty array the field is deleted from the values

Expected behavior

expected to set the field value to be an empty array

Reproducible example

https://codesandbox.io/s/formik-codesandbox-template-leltv

Your environment

Software Version(s)
Formik 2.0.7
React 16.12.0
TypeScript
Browser chrome
npm/Yarn yarn
Operating System linux
rodymolenaar commented 4 years ago

This was introduced in 2.0.7 probably in https://github.com/jaredpalmer/formik/commit/9da32887cf61bdb2d08be90d863b35d537ebfe7c.

eugenemlee commented 4 years ago

@wingedearth Yes, this change has broken all usage of FieldArrays. Could it please be reverted?

jaredpalmer commented 4 years ago

Going to revert now.

wingedearth commented 4 years ago

Simply reverting will bring back the bug of errors: { someField: [] } caused isValid to be false when it should be true for FieldArrays

wingedearth commented 4 years ago

so the field with the empty array should be preserved for values but removed from errors (if there are no errors)

wabi91 commented 4 years ago

I have an another issue, i think something changed and it makes empty array issue and my issue. When use setFieldValue passing same length array what is not deep equal array with prev array, "FastField" component doesn't re-render. It was not happened with version 2.0.6.

aleksavl commented 4 years ago

not fixed with latest release

Krasnopir commented 4 years ago

I also returned to version 06. because all data in the form of an array is deleted if an empty array is returned - chips, radioGroup, etc...

alexe-dev commented 4 years ago

hi. any updates on this?

tylerwray commented 4 years ago

FYI, this also breaks when explicitly setting a value to []

setFieldValue('stuff', []) sets the field stuff to undefined, which in our case, is an unwanted/breaking behavior. Should prob be a major version change for that.

Thanks for all the awesome work on Formik. I ❤️ forms in React now b/c of Formik.

bugzpodder commented 4 years ago

repro https://codesandbox.io/s/formik-example-934n6 but you probably don't need it.

YufeeXing commented 4 years ago

it seems that 2.0.3 and 2.0.8 have the same problem

DalerAsrorov commented 4 years ago

Still facing the same issue.

viniciusdacal commented 4 years ago

@DalerAsrorov have you tried 2.0.11? It should be fixed in that version.

donperi commented 4 years ago

I'm facing the same issue on 2.0.11. Using setFieldValue(field.name, []) deletes the field from the form.

I can't use <FieldArray /> neither, as it doesn't allow custom props as <Field /> does.

Ex:

<FieldArray
   component={CustomComponent}
   customPropForCustomCompoent="value"
/>

customPropForCustomCompoent will be missed.

viniciusdacal commented 4 years ago

@donperi can you make a sandbox reproducing the issue? I tried this one and seems to be ok on 2.0.11 https://codesandbox.io/s/formik-example-6021m (Change the email and see the props output)

donperi commented 4 years ago

Sure @viniciusdacal, but give me some time. I'm busy with another stuff atm.

jaredpalmer commented 4 years ago

Can someone make a PR?

jaredpalmer commented 4 years ago

Im out of office this week

viniciusdacal commented 4 years ago

I tested on latest version and it's working ok for me. If we manage to get a sandbox which we can repro the issue, I can work on it.

zsobral commented 4 years ago

I'm using 2.1.1 now, the issue was solved for me

DalerAsrorov commented 4 years ago

I will upgrade to 2.1.1 and see if it works for me too.

jaredpalmer commented 4 years ago

I believe this was fixed in the latest release

alegri1 commented 4 years ago

Just updated to 2.1.1 now and can confirm that the issue is resolved for me

Krasnopir commented 4 years ago

confirm - everything is ok