Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): keep onSubmit error when blurring an unchanged field #2211

Openchatman-media(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/2211/headopened

Closes #1242

Problem

A field-level onSubmit error is wrongly cleared when the user focuses and blurs the field without changing its value. The submit error should only disappear once the user actually enters a (valid) value.

Root cause

In FieldApi, the logic that clears a stored submit error fired for any cause other than 'submit':

this.state.meta.errorMap?.[submitErrKey] &&
cause !== 'submit' &&
!hasErrored

handleBlur runs validation with cause: 'blur', which satisfied cause !== 'submit', so a plain blur (no edit) cleared the submit error.

Fix

Clear the stored submit error only on an actual value change:

cause === 'change' &&

A blur (or any non-value cause) no longer drops the submit error. Entering a valid value still clears it as documented.

Tests

Two tests added to packages/form-core/tests/FieldApi.spec.ts:

  • should not clear onSubmit errors on blur when the value did not change — fails on main, passes with the fix
  • should clear onSubmit errors once a valid value is entered — guards the documented clear-on-change path

Full form-core suite green (107 tests).

Summary by CodeRabbit

  • Bug Fixes

    • Fixed form validation to properly preserve submit errors during blur and non-value interactions. Submit errors are now cleared only when valid values are entered, improving form error feedback accuracy.
  • Tests

    • Extended test coverage for submit error behavior, verifying errors persist on blur without changes and clear when valid values are entered.
    1. 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

      Run ID: 5e7097f8-a41c-492e-b209-cf17699f7708

      📥 Commits

      Reviewing files that changed from the base of the PR and between 6a73479f2c2659550a6c8b456e3e139bf0d2142e and 1d7dbe6aea9e0d9e4e1b81e1d6d56a6bb58752af.

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

      📝 Walkthrough

      Walkthrough

      FieldApi.validateSync is updated to clear the stored onSubmit error only when cause === 'change', instead of when cause !== 'submit'. Two tests are added: one confirming the error persists after a post-submit blur, and one confirming it clears after a valid handleChange.

      Changes

      onSubmit error lifecycle fix

      Layer / File(s)Summary
      validateSync condition and tests
      packages/form-core/src/FieldApi.ts, packages/form-core/tests/FieldApi.spec.ts
      The clearing condition is narrowed to cause === 'change' with an updated comment; two new async tests verify that onSubmit errors survive a blur and are removed only when a valid value is entered via handleChange.

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~8 minutes

      Poem

      🐇 Hop, hop — the blur once wiped my error away, But now I guard submit mistakes come what may! A change of heart? I'll clear the slate with glee. A sneaky blur? The error stays, you see. No more silent vanishing acts from me! ✨

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title accurately summarizes the main change: keeping onSubmit errors when blurring unchanged fields.
      Description check✅ PassedThe PR description comprehensively covers the problem, root cause, fix, and testing, meeting the template's core requirements.
      Linked Issues check✅ PassedThe PR fully addresses issue #1242 by fixing the bug where submit errors are incorrectly cleared on blur without value changes.
      Out of Scope Changes check✅ PassedAll changes are focused on fixing the submit error clearing behavior in FieldApi and adding related test coverage; no unrelated modifications present.
      Docstring Coverage✅ PassedNo 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.
    2. Checked ValidationCause and it's 'change' | 'blur' | 'submit' | 'mount' | 'server' | 'dynamic', so the old cause !== 'submit' was clearing a stale onSubmit error on blur, mount, server and dynamic validations too, not just on an actual value change. cause === 'change' is the right fix, it only clears the error for the one cause that actually means "the user entered something new," which is what the surrounding comment already claimed the code was doing.

      The two new tests cover the pair that matters, blurring an untouched field keeps the error, entering a valid value clears it, and neither the FieldApi type nor any caller elsewhere seems to rely on the old broader clearing behavior.

Sign in to join this conversation.