Skip to content

fix(form-core): clear stale onBlur errors on re-submission#2120

Open
restareaByWeezy wants to merge 2 commits intoTanStack:mainfrom
restareaByWeezy:fix/2034-field-level-validation-on-submit
Open

fix(form-core): clear stale onBlur errors on re-submission#2120
restareaByWeezy wants to merge 2 commits intoTanStack:mainfrom
restareaByWeezy:fix/2034-field-level-validation-on-submit

Conversation

@restareaByWeezy
Copy link
Copy Markdown

@restareaByWeezy restareaByWeezy commented Apr 11, 2026

Changes

Fixes #2034

When a field has an onBlur validator that produces an error, subsequent submit attempts get blocked. The canSubmit check in _handleSubmit returns early before validateAllFields has a chance to re-run and clear the stale error.

This patch skips the early return on re-submission (submissionAttempts > 1) so that validateAllFields actually runs again. First-submission behavior is unchanged.

Added a regression test that reproduces the exact scenario from the issue — conditional field validation with type switching.

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

    • Stale field-level validation errors from prior onBlur validation are now cleared on resubmission; fields are re-validated so subsequent submits reflect current errors.
  • Tests

    • Added a regression test ensuring onBlur validators re-run and stale errors don't block a later successful submit.
  • Chores

    • Prepared a patch release note marking this fix.

…errors

When a field has an onBlur validator that produces an error, subsequent
form submission attempts are blocked by a canSubmit check that runs
before validateAllFields. This means stale onBlur errors (e.g. from a
field that is no longer invalid) prevent re-submission.

Fix: on re-submission attempts (submissionAttempts > 1), skip the early
canSubmit return so validateAllFields can re-run all field validators for
the submit cause. Stale errors are cleared and the isFieldsValid check
below handles the invalidity gate correctly.

First-submission behavior (submissionAttempts <= 1) is unchanged so
existing canSubmit semantics (e.g. onMount errors) are preserved.

Fixes TanStack#2034
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fdf55fab-12d4-486e-bd7f-66c080186cbf

📥 Commits

Reviewing files that changed from the base of the PR and between 01ee70b and 5145dec.

📒 Files selected for processing (1)
  • .changeset/fix-stale-onblur-resubmit.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/fix-stale-onblur-resubmit.md

📝 Walkthrough

Walkthrough

Updated FormApi._handleSubmit to avoid skipping validation on subsequent submission attempts: when !this.state.canSubmit and not forced by _devtoolsSubmissionOverride, the method now returns early only for the first submission attempt. Later attempts continue to re-run field-level validators so stale onBlur errors can be cleared before deciding submission outcome.

Changes

Cohort / File(s) Summary
FormApi Submission Logic
packages/form-core/src/FormApi.ts
Changed _handleSubmit early-exit behavior: only the first invalid submit returns early and calls onSubmitInvalid; subsequent submits proceed to re-run field validators and then determine whether to call onSubmit or onSubmitInvalid.
FormApi Test Coverage
packages/form-core/tests/FormApi.spec.ts
Added regression test that verifies onBlur field validators re-run on re-submission and that stale onBlur errors are cleared when field conditions change.
Release Notes / Changeset
.changeset/fix-stale-onblur-resubmit.md
Added a changeset entry describing the patch: "fix(form-core): clear stale onBlur errors on re-submission".

Sequence Diagram(s)

mermaid
sequenceDiagram
participant User
participant FormApi
participant FieldValidator
participant SubmitHandlers
User->>FormApi: trigger handleSubmit()
FormApi->>FormApi: check state.canSubmit and _devtoolsSubmissionOverride
alt first submission and can't submit
FormApi->>SubmitHandlers: call onSubmitInvalid()
SubmitHandlers-->>User: notify invalid
else subsequent submission or can submit
FormApi->>FieldValidator: validateAllFields('submit')
FieldValidator-->>FormApi: update field errors (clears stale onBlur errors)
FormApi->>FormApi: isFieldsValid()
alt valid
FormApi->>SubmitHandlers: call onSubmit()
SubmitHandlers-->>User: submit success
else invalid
FormApi->>SubmitHandlers: call onSubmitInvalid()
SubmitHandlers-->>User: notify invalid
end
end

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped upon a submit and learned to try again,
Old blur-born errors loosened from their hold,
Validators woke and cleared the stain,
The form at last could do what’s bold. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing stale onBlur errors being cleared on re-submission, which directly addresses the core issue.
Description check ✅ Passed The description follows the template structure with all required sections completed: Changes section explains the fix and issue reference, Checklist items are checked, and Release Impact is properly documented with changeset confirmation.
Linked Issues check ✅ Passed The PR directly addresses issue #2034 by implementing field-level validation re-runs on re-submission to clear stale onBlur errors, matching the expected behavior described in the issue.
Out of Scope Changes check ✅ Passed All changes are in-scope: FormApi.ts modification fixes the submission logic, FormApi.spec.ts adds a regression test, and the changeset documents the fix—all directly related to resolving issue #2034.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

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.

Field-level validation does not run on submit.

1 participant