Skip to content

fix: validate consecutive setFieldValue calls correctly - #4084

Open
kanza-shahid-dev wants to merge 1 commit into
jaredpalmer:mainfrom
kanza-shahid-dev:fix/setfieldvalue-validation-3754
Open

kanza-shahid-dev wants to merge 1 commit into
jaredpalmer:mainfrom
kanza-shahid-dev:fix/setfieldvalue-validation-3754

Conversation

@kanza-shahid-dev

Copy link
Copy Markdown

Description

Fixes #3754

Fixes an issue where multiple consecutive setFieldValue calls could trigger validation using stale form values.

When setFieldValue was called multiple times asynchronously, the validation triggered by a later call could use an outdated state.values. This could result in stale validation errors even though the form values had already been updated.

Changes

  • Build the validation values from stateRef.current.values when handling setFieldValue.
  • Pass those current values to validateFormWithHighPriority.
  • Add a regression test covering multiple asynchronous setFieldValue calls with a Yup validation schema.

Testing

  • yarn test Formik.test.tsx — 89 tests passed
  • yarn test — 227 tests passed
  • git diff --cached --check — passed

@changeset-bot

changeset-bot Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2208577

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
formik Patch
formik-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Sep 17, 2026

Copy link
Copy Markdown

@kanza-shahid-dev is attempting to deploy a commit to the Formik Team on Vercel.

A member of the Team first needs to authorize it.

@codesandbox-ci

codesandbox-ci Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@kanza-shahid-dev
kanza-shahid-dev force-pushed the fix/setfieldvalue-validation-3754 branch from 77a06eb to 2208577 Compare September 17, 2026 11:11

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation input now comes from stateRef.current.values, but functional updater values still read state.values from the last render. If setFieldValue('count', n => n + 1) is called twice before React rerenders, dispatch advances stateRef.current after the first call, yet the second updater still receives the original count; the field ends at 1 instead of 2. Could the updater read stateRef.current.values too, with a same-tick functional-update regression test? I reviewed the diff; I did not run tests locally.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Multiple calls to setFieldValue inside async action result in form errors

2 participants