fix(form-core): keep onSubmit error when blurring an unchanged field - #2211
fix(form-core): keep onSubmit error when blurring an unchanged field#2211chatman-media wants to merge 1 commit into
Conversation
The submit-error auto-clear in FieldApi.validateSync fired on every non-submit validation cause, including blur. As a result, focusing and leaving a field without editing it dropped a valid submit error. Gate the clear on a value `change` cause so the error is only removed when the user actually enters a new value, matching the documented intent of the block. Closes TanStack#1242
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesonSubmit error lifecycle fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
MILLERMARRU
left a comment
There was a problem hiding this comment.
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.
Closes #1242
Problem
A field-level
onSubmiterror 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 storedsubmiterror fired for any cause other than'submit':handleBlurruns validation withcause: 'blur', which satisfiedcause !== 'submit', so a plain blur (no edit) cleared the submit error.Fix
Clear the stored submit error only on an actual value
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 onmain, passes with the fixshould clear onSubmit errors once a valid value is entered— guards the documented clear-on-change pathFull
form-coresuite green (107 tests).Summary by CodeRabbit
Bug Fixes
Tests