Skip to content

fix(form-core): create complete field meta on setFieldValue for uninitialized fields#2245

Open
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/setfieldvalue-uninitialized-meta-1078
Open

fix(form-core): create complete field meta on setFieldValue for uninitialized fields#2245
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/setfieldvalue-uninitialized-meta-1078

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

What & why

FormApi.setFieldValue on a field that has never mounted built its meta with (prev) => ({ ...prev, isTouched, isDirty, errorMap }) where prev is undefined, producing a partial meta object missing required fields declared on AnyFieldLikeMetaBase (isValidating, isBlurred, errorSourceMap, _arrayVersion, _pendingValidationsCount). This is the incomplete-meta problem reported in #1078.

Fix

Default the updater argument to the shared defaultFieldMeta:

(prev = defaultFieldMeta) => ({ ...prev, isTouched, isDirty, errorMap })

in packages/form-core/src/FormApi.ts, mirroring the exact pattern already used at the two other setFieldMeta call sites in the same file. The meta created for a never-mounted field is now structurally identical to a mounted field's.

Tests

packages/form-core/tests/FormApi.spec.ts — added a failing-first test asserting the created meta is complete (isValidating: false, isBlurred: false, errors: [], errorSourceMap: {}, no undefined fields) after setFieldValue on an uninitialized field. Confirmed red on original code → green after the fix.

Full @tanstack/form-core suite: 504 passed / 3 todo, no type errors.

Closes #1078

Summary by CodeRabbit

  • Bug Fixes

    • Improved field updates for fields that have not been mounted yet.
    • Ensured complete and consistent field metadata is created, including validation and interaction state.
  • Tests

    • Added coverage verifying metadata remains fully populated when setting an unmounted field’s value.

…tialized fields

When `setFieldValue` targets a field that has not been mounted yet (e.g. a
conditionally rendered field pre-populated before render), the meta updater
spread an `undefined` previous meta, producing a partial object missing
required fields such as `isValidating`, `isBlurred` and `errorSourceMap`.

Default the previous meta to `defaultFieldMeta` so the newly created meta is
structurally complete and consistent with a normally mounted field, matching
the pattern already used by other `setFieldMeta` call sites.

Closes TanStack#1078
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

FormApi.setFieldValue now creates complete metadata when updating an unmounted field, including default error-related properties. A regression test verifies the resulting metadata shape and defined validation fields.

Changes

Field metadata initialization

Layer / File(s) Summary
Default metadata update and regression coverage
packages/form-core/src/FormApi.ts, packages/form-core/tests/FormApi.spec.ts
The metadata updater defaults missing previous metadata to defaultFieldMeta, while tests verify complete metadata for an unmounted field.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

  • TanStack/form#2143: Updates setFieldValue metadata initialization and related default metadata keys.

Suggested reviewers: crutchcorn, pascalmh, kyujenius

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: fixing complete meta creation for uninitialized fields in form-core.
Description check ✅ Passed The description covers the change, motivation, fix, and tests, though it omits some template sections like checklist and release impact.
Linked Issues check ✅ Passed The code and regression test address #1078 by ensuring setFieldValue creates complete field meta, including errors, before initialization.
Out of Scope Changes check ✅ Passed The PR stays focused on the meta initialization bug and its test coverage, with no unrelated changes shown.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/form-core/tests/FormApi.spec.ts (1)

102-110: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Also assert touched/dirty state preservation.

The production change explicitly preserves isTouched and isDirty, but this regression test does not verify either field.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/form-core/tests/FormApi.spec.ts` around lines 102 - 110, Extend the
metadata assertions in FormApi.spec.ts to verify that the preserved isTouched
and isDirty values remain defined and retain their expected state alongside
isValidating, isBlurred, and errorSourceMap.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/form-core/tests/FormApi.spec.ts`:
- Line 92: Update the test around form.mount() to capture its returned cleanup
function and invoke that cleanup in a finally block, ensuring listeners are
removed even when the test fails.

---

Nitpick comments:
In `@packages/form-core/tests/FormApi.spec.ts`:
- Around line 102-110: Extend the metadata assertions in FormApi.spec.ts to
verify that the preserved isTouched and isDirty values remain defined and retain
their expected state alongside isValidating, isBlurred, and errorSourceMap.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8aef8188-71ed-4945-96e1-c8181d1d2a35

📥 Commits

Reviewing files that changed from the base of the PR and between 5d11281 and 81c50db.

📒 Files selected for processing (2)
  • packages/form-core/src/FormApi.ts
  • packages/form-core/tests/FormApi.spec.ts

availableFor: 'longStay',
} as { availableFor: string; fees?: number },
})
form.mount()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Clean up the form mount in the test.

form.mount() registers listeners and returns a cleanup function. Capture and invoke it in finally to prevent cross-test listener leaks.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/form-core/tests/FormApi.spec.ts` at line 92, Update the test around
form.mount() to capture its returned cleanup function and invoke that cleanup in
a finally block, ensuring listeners are removed even when the test fails.

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.

form.setFieldValue on an un-initialized field will create meta missing errors

1 participant