Skip to content

fix(form-core): apply async defaultValues to untouched fields after other fields are edited#2246

Open
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/async-default-untouched-fields-2229
Open

fix(form-core): apply async defaultValues to untouched fields after other fields are edited#2246
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/async-default-untouched-fields-2229

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

Fixes #2229. FormApi.update() previously gated default-value merging on the form-level touched state, so touching one field prevented asynchronously arriving defaults from applying to every other untouched field.

  • Drop the form-level touched gate and merge updated defaults per field instead.
  • Preserve user-edited values for touched fields.
  • Apply new default values to untouched fields, including fields that have not mounted yet.
  • Add regression coverage for both scalar fields and array fields.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.

Notes

The existing behavior of not wiping an edited field during update is preserved. The new behavior only applies newly arrived defaults to fields that are still untouched.

…dits

FormApi.update() gated the whole defaultValues merge on the form-level
`isTouched` flag, which is true as soon as any single field is edited.
As a result, once the user touched one field, asynchronously-arriving
default values were no longer applied to *any* field, leaving untouched
fields empty.

Drop the form-level gate and instead preserve values per-field: edited
(touched) fields keep the user's input, while untouched fields — including
ones that have not mounted yet — receive their newly-arrived defaults.

Closes TanStack#2229
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

FormApi.update now merges incoming default values with current values, preserving touched fields while updating untouched fields. Tests cover scalar and array defaults, and a patch changeset documents the fix.

Changes

Async default value updates

Layer / File(s) Summary
Preserve edits during default updates
packages/form-core/src/FormApi.ts, .changeset/async-default-untouched-fields.md
FormApi.update applies changed defaults to untouched fields while retaining current values for touched fields.
Validate scalar and array defaults
packages/form-core/tests/FormApi.spec.ts
Tests verify that scalar and array defaults update correctly without overwriting edited values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • TanStack/form#2190: Both changes address async defaultValues updates, including array-field update behavior.

Suggested reviewers: crutchcorn

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The code and tests satisfy #2229 by merging async defaults per field and preserving edited values.
Out of Scope Changes check ✅ Passed The changes stay within the issue scope: implementation fix, regression tests, and a changeset entry.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly matches the main change: async defaultValues now apply to untouched fields after edits.
Description check ✅ Passed The description follows the template and includes changes, checklist, release impact, and a changeset note.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Async default values not applied to untouched fields after other fields are edited

1 participant