Skip to content

fix: create fieldMeta on async validation for fields with errors - #2056

Merged
crutchcorn merged 5 commits into
TanStack:mainfrom
literalpie:literalpie/async-validation-unmounted
Jul 9, 2026
Merged

fix: create fieldMeta on async validation for fields with errors#2056
crutchcorn merged 5 commits into
TanStack:mainfrom
literalpie:literalpie/async-validation-unmounted

Conversation

@literalpie

@literalpie literalpie commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

This updates the validateAsync function to have similar logic to validateSync. See #1691 where this logic was added to validateSync.

This makes it so that if async validation results in errors, any fields with errors will have a fieldMeta created

Fixes #2033

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed async validation so field metadata is created/updated for fields that have errors returned from async form validation, even when metadata wasn’t previously initialized.
    • Improved how per-field async validation merges error information, ensuring error messages are correctly reflected in field-level metadata.
  • Tests
    • Added coverage to verify { fields: ... } async submission errors are converted into the expected field metadata entries.

This updates the validateAsync function to have similar logic to validateSync. See TanStack#1691 where this logic was added to validateSync.

Fixes TanStack#2033
@changeset-bot

changeset-bot Bot commented Mar 2, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0868ac1

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

This PR includes changesets to release 13 packages
Name Type
@tanstack/form-core Patch
@tanstack/angular-form Patch
@tanstack/form-devtools Patch
@tanstack/lit-form Patch
@tanstack/react-form Patch
@tanstack/solid-form Patch
@tanstack/svelte-form Patch
@tanstack/vue-form Patch
@tanstack/react-form-devtools Patch
@tanstack/solid-form-devtools Patch
@tanstack/react-form-nextjs Patch
@tanstack/react-form-remix Patch
@tanstack/react-form-start 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

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR fixes async validation so fields that only receive form-level async errors still get fieldMeta created. It also adds a test covering submit-time async validation output shaped as { fields: ... }.

Changes

Fix fieldMeta creation during async validation

Layer / File(s) Summary
Async validation field meta initialization
packages/form-core/src/FormApi.ts, .changeset/shy-otters-give.md
validateAsync now processes field names from both existing fieldMeta keys and fieldErrorsFromFormValidators, skips only when neither meta nor errors exist, and initializes missing meta from defaultFieldMeta before merging error maps. A changeset entry records the patch release note.
Test async form submit validation fieldMeta creation
packages/form-core/tests/FormApi.spec.ts
New async submit-validation test verifies a { fields: ... } error result creates fieldMeta.age and sets errorMap.onSubmit after handleSubmit() and timer advancement.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant FormApi
  participant onSubmitAsync
  participant fieldMeta

  FormApi->>onSubmitAsync: run async submit validation
  onSubmitAsync-->>FormApi: return { fields: { age: error } }
  FormApi->>FormApi: include fieldErrorsFromFormValidators in field list
  FormApi->>fieldMeta: create/update age meta with errorMap.onSubmit
Loading

Suggested reviewers: crutchcorn

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR directly addresses issue #2033 by implementing the expected behavior where async validation creates fieldMeta for affected fields, matching synchronous validation behavior.
Out of Scope Changes check ✅ Passed All changes are scoped to the async validation fix: a changeset entry, FormApi.ts logic updates, and corresponding test coverage with no unrelated modifications.
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: creating fieldMeta during async validation for fields with errors.
Description check ✅ Passed The description follows the template with Changes, Checklist, and Release Impact sections filled in.
✨ 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.

@literalpie

literalpie commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

@crutchcorn I hate to be the guy who pings the OSS maintainer, but I really think this fix is worth getting merged. We've been using a patch of this change for a few months in our large app without issue. Please let me know if there's anything I can do to help.

Based on reactions and commit references on this PR and the issue, I think it would benefit others too.

@nx-cloud

nx-cloud Bot commented Jul 9, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit af28b12

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 3m 18s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 40s View ↗

☁️ Nx Cloud last updated this comment at 2026-07-09 09:53:20 UTC

# Conflicts:
#	packages/form-core/tests/FormApi.spec.ts
@pkg-pr-new

pkg-pr-new Bot commented Jul 9, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@2056

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@2056

@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@2056

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@2056

@tanstack/preact-form

npm i https://pkg.pr.new/@tanstack/preact-form@2056

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@2056

@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@2056

@tanstack/react-form-nextjs

npm i https://pkg.pr.new/@tanstack/react-form-nextjs@2056

@tanstack/react-form-remix

npm i https://pkg.pr.new/@tanstack/react-form-remix@2056

@tanstack/react-form-start

npm i https://pkg.pr.new/@tanstack/react-form-start@2056

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@2056

@tanstack/solid-form-devtools

npm i https://pkg.pr.new/@tanstack/solid-form-devtools@2056

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@2056

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@2056

commit: 1fd754f

crutchcorn added a commit that referenced this pull request Jul 9, 2026
…d field (#2233)

* fix(form-core): guard FormGroupApi.distributeFieldErrors against unmounted field meta

FormGroupApi.distributeFieldErrors called setFieldMeta with an updater
that unconditionally spread prev, which is undefined when a group
validator returns fields errors for a sub-field with no fieldMetaBase
entry yet. Apply the same (prev = defaultFieldMeta) guard already used
in FormApi's form-level field-error fan-out (see #1706, #2056).

Fixes #2231

* chore: add changeset for #2231

---------

Co-authored-by: Corbin Crutchley <git@crutchcorn.dev>
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.72%. Comparing base (6892ed0) to head (1fd754f).
⚠️ Report is 247 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2056      +/-   ##
==========================================
- Coverage   90.35%   89.72%   -0.63%     
==========================================
  Files          38       65      +27     
  Lines        1752     3203    +1451     
  Branches      444      809     +365     
==========================================
+ Hits         1583     2874    +1291     
- Misses        149      295     +146     
- Partials       20       34      +14     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@crutchcorn
crutchcorn merged commit 6cd477a into TanStack:main Jul 9, 2026
8 of 9 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 9, 2026
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.

fieldMeta not set by async validator

3 participants