Skip to content

fix(form-core): re-run form-level validator on resubmit to clear its own stale field errors - #2260

Open
FrancoKaddour wants to merge 2 commits into
TanStack:mainfrom
FrancoKaddour:fix/form-level-validator-resubmit
Open

fix(form-core): re-run form-level validator on resubmit to clear its own stale field errors#2260
FrancoKaddour wants to merge 2 commits into
TanStack:mainfrom
FrancoKaddour:fix/form-level-validator-resubmit

Conversation

@FrancoKaddour

@FrancoKaddour FrancoKaddour commented Jul 31, 2026

Copy link
Copy Markdown

Closes #2248

Problem

With only a form-level onSubmit validator (no field-level validators), the form can never re-validate on a second submit while any error is outstanding.

_handleSubmit runs validateAllFields('submit') (field-level only), then bails at if (!this.state.isFieldsValid) return — before this.validate('submit') (the form-level validator) is reached. A form-level validator writes its errors onto fields ({ fields }), but validateAllFields can't clear or recompute them, so isFieldsValid stays false from the previous submit and the form-level validator never re-runs — gating the form off permanently (even with canSubmitWhenInvalid: true).

Fix

Run the form-level validator right after validateAllFields, guarded by whether any field-level validator errored this submit (validateAllFields returns those errors). When none errored, it re-runs and recomputes/clears its own field errors; when field-level validators did error, it's skipped as before (preserving the existing "don't trigger form validators if field validators errored" behavior).

Tests

Added a form-core unit test reproducing #2248 (form-level-only validator, fill fields, resubmit → errors clear and onSubmit runs). Full form-core (504) and react-form (126) suites pass; the existing field-validator-gating test still passes.

Summary by CodeRabbit

  • Bug Fixes
    • Form-level submit validation now reruns on each submission when field-level validation passes.
    • Stale field errors are cleared after previously invalid fields are corrected.
    • Valid forms can now submit successfully on a subsequent attempt.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4fb62140-46e5-4d3e-aabc-76e0f15f915c

📥 Commits

Reviewing files that changed from the base of the PR and between 7840e02 and 95847a7.

📒 Files selected for processing (1)
  • packages/form-core/tests/FormApi.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/form-core/tests/FormApi.spec.ts

📝 Walkthrough

Walkthrough

Form submission now reruns form-level validation when field validation has no errors. A regression test verifies that corrected values clear stale errors and allow onSubmit to run. A patch changeset documents the behavior.

Changes

Form validation resubmission

Layer / File(s) Summary
Conditional submission validation
packages/form-core/src/FormApi.ts
Submission stores field-validation errors, runs form-level validation only when no field errors exist, and removes the unconditional later validation call.
Regression coverage and release note
packages/form-core/tests/FormApi.spec.ts, .changeset/form-level-validator-resubmit.md
The regression test verifies that a second submission clears stale errors and invokes onSubmit. The changeset documents the patch release.

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

Possibly related issues

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the form-level validator resubmission fix and its stale-error behavior.
Description check ✅ Passed The description clearly explains the problem, fix, tests, and release impact, although it does not use the repository template headings or checklist.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

@FrancoKaddour
FrancoKaddour marked this pull request as ready for review August 3, 2026 12:30

@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

🤖 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`:
- Around line 1974-1977: Update the regression comment in the FormApi test
around _handleSubmit to replace “Currently” with “Before this fix,” preserving
the description that the prior implementation bailed before validate('submit')
and documenting it as historical behavior.
🪄 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 Plus

Run ID: 451a049e-47dc-4dc9-a1d9-487de253cc4d

📥 Commits

Reviewing files that changed from the base of the PR and between 5d11281 and 7840e02.

📒 Files selected for processing (3)
  • .changeset/form-level-validator-resubmit.md
  • packages/form-core/src/FormApi.ts
  • packages/form-core/tests/FormApi.spec.ts

Comment thread packages/form-core/tests/FormApi.spec.ts Outdated
Per review: the comment documented the bug in present tense, which reads as
current behavior now that the fix is in place.
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-level validator doesn't re-run after first submit while fields hold errors — bug or by design?

1 participant