formik: setFieldValue to undefined deletes the value from values and prevents errors from showing / touched being updated on submit
š Bug report
Current Behavior
When setFieldValue() is called to set a field to undefined, Formik deletes it from values. The consequence of this is that Formik no longer marks the field as touched on submit and therefore error messages for the field no longer show (using <ErrorMessage>)
Steps to repro:
- Formik state after submit is clicked and the input field is not changed by the user. As you can see the error message is visible and touched is automatically set.

- Formik state after (a) input field is changed to a value, then (b) the input field is cleared, and then Ā© submit is clicked again. The nuance here is that when the field is cleared,
setFieldValueis called to set the form field to undefined. You can see the error exists, but the field name is no longer in values and touched does not have the field name either.

Expected behavior
On submit, Formik should continue to mark ALL the keys in initialValues as touched.
Reproducible example
Suggested solution(s)
Two potential solutions (a) Formik should set the field value to undefined instead of deleting it from values when setFieldValue is called with the value of undefined OR (b) Formik should clone the keys from initial values and use that to determine which fields to touch on submit instead of the current set of keys in values.
Additional context
None
Your environment
| Software | Version(s) |
|---|---|
| Formik | 2.1.1 (and 1.1.1) |
| React | 16.12.0 |
| TypeScript | n/a |
| Browser | Chrome Version 80.0.3987.122 (Official Build) (64-bit) |
| npm/Yarn | Yarn |
| Operating System | Mac OS X |
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 37
- Comments: 15
Still unresolved in 2023?
@zyofeng Youāre right, my apologies. I failed to paste the important part, which was a replacement of setIn that I was using that changes the default setIn behavior. Iāve updated my original comment to include it.
I got the same problem. The onChange function set field value to undefined. And then the form shows no error message. Because the errors and values object has delete the field key. 2023-11-02 I am crazy.
also, this is the issue when using
initialValuesand you donāt use all values, user click āsubmitā and we never show error on fields that are emptym because ofundefinedThis doesnāt work as setIn is the one that removes the obj if value is undefined. see line 130
https://github.com/formium/formik/blob/2d613c11a67b1c1f5189e21b8d61a9dd8a2d0a2e/packages/formik/src/utils.ts
For anyone who has run into this issue and needs a workaround, one solution is to create your own wrapper around Formikās setFieldValue function to override its behavior. You can do this by either wrapping Formik entirely (which is more work but allows for more seamless integration) or just calling a surrogate function manually.
Here is what a surrogate function would look like:
I agree, formik should be setting the value to undefined, not deleting the property from the values object. Many operations in Formik rely on the existence of the entry in the values object in order to know what is errored/touched, and by deleting, it is wiping that data.
These lines seem to be the culprits, although there may be others: https://github.com/jaredpalmer/formik/blob/master/packages/formik/src/utils.ts#L131 https://github.com/jaredpalmer/formik/blob/master/packages/formik/src/utils.ts#L139