jaredpalmer / formik

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

Reducer error: calling setFieldTouched and setFieldValue in a row will validate wrong #3688

Open fabb opened 1 year ago

fabb commented 1 year ago

Bug report

Current Behavior

In the onBlur handler of an <input>, when manually calling setFieldValue and setFieldTouched in this order, and only doing validation on setFieldTouched, an outdated value will be validated.

Expected behavior

The order of setFieldValue and setFieldTouched should be irrelevant when the shouldValidate parameter is set to true only on the last one of the two calls.

Reproducible example

In the example, type in some value in the text field and then blur it. On blur, a different value is set to the state, but validation uses the outdated value before the state has been set.

https://codesandbox.io/s/formik-validation-bug-1ot7yy

Suggested solution(s)

Not sure, probably the reducer needs to be fixed.

Additional context

It used to work correctly in an older Formik version (e.g. 1.2.0).

Your environment

Software Version(s)
Formik 2.2.9
React 18.2.0
TypeScript -
Browser Chrome 107
npm 8.15.0
Operating System macOS Ventura
1aerostorm commented 5 months ago

Looks like https://github.com/jaredpalmer/formik/issues/2457 And our solution: https://github.com/jaredpalmer/formik/issues/2457#issuecomment-2165499156