fix(form-core): re-run form-level validator on resubmit to clear its own stale field errors - #2260
Conversation
…own stale field errors
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughForm submission now reruns form-level validation when field validation has no errors. A regression test verifies that corrected values clear stale errors and allow ChangesForm validation resubmission
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
.changeset/form-level-validator-resubmit.mdpackages/form-core/src/FormApi.tspackages/form-core/tests/FormApi.spec.ts
Per review: the comment documented the bug in present tense, which reads as current behavior now that the fix is in place.
MILLERMARRU
left a comment
There was a problem hiding this comment.
Traced through validateAllFields and validate on main to make sure I understood what each one actually touches before judging the fix. validateAllFields('submit') calls every field's validate with skipFormValidation: true, so fieldErrors here is strictly field-level validator output, it never includes anything the form-level { fields } validator wrote. validate('submit') is the one that actually runs options.validators.onSubmit against the whole form and applies its fields map onto each field's error map. That separation is what makes the fix work.
The actual deadlock in #2248 makes sense once you see that isFieldsValid is a stale aggregate: after the first submit, the form-level validator writes Required into fieldMeta.name.errorMap.onSubmit and fieldMeta.other. On the second submit, there are no field-level validators to touch those entries, so isFieldsValid is still reading last submit's form-level output at the point the old code checked it, before validate('submit') ever got a chance to re-run and overwrite it. Gating the early-return on this.state.isFieldsValid was gating on data the form-level validator itself was supposed to refresh.
Switching the condition to fieldErrors.length === 0 fixes it because that's always this submit's fresh result, not a carried-over flag. And behaviorally nothing changes for the case that matters, when a field-level validator does fail, fieldErrors.length > 0 skips the form-level validator exactly like the old isFieldsValid gate did, since a field-level failure alone was already enough to fail isFieldsValid before. The only path that changes is the one that was actually broken, field-level clean but form-level state stale, and now the form-level validator gets to run and clear its own mess before the gate checks anything.
I also want to flag that the second await this.validate('submit') further down wasn't duplicated, it was moved, so the form-level validator still runs at most once per submit attempt in the passing path. That's an easy thing to get subtly wrong when relocating a call like this, and it's clean here.
Test is solid, it reproduces the exact regression path (submit empty, see fields get flagged by the form-level validator, fill the fields, submit again) and asserts both that the stale errors clear and that onSubmit actually fires, which is the part that would have silently failed forever under the old code without ever throwing.
|
Thanks for the careful trace @MILLERMARRU — you nailed exactly why gating on this submit's fresh |
Closes #2248
Problem
With only a form-level
onSubmitvalidator (no field-level validators), the form can never re-validate on a second submit while any error is outstanding._handleSubmitrunsvalidateAllFields('submit')(field-level only), then bails atif (!this.state.isFieldsValid) return— beforethis.validate('submit')(the form-level validator) is reached. A form-level validator writes its errors onto fields ({ fields }), butvalidateAllFieldscan't clear or recompute them, soisFieldsValidstaysfalsefrom the previous submit and the form-level validator never re-runs — gating the form off permanently (even withcanSubmitWhenInvalid: true).Fix
Run the form-level validator right after
validateAllFields, guarded by whether any field-level validator errored this submit (validateAllFieldsreturns 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-coreunit test reproducing #2248 (form-level-only validator, fill fields, resubmit → errors clear andonSubmitruns). Fullform-core(504) andreact-form(126) suites pass; the existing field-validator-gating test still passes.Summary by CodeRabbit