diff --git a/.changeset/wet-geese-reply.md b/.changeset/wet-geese-reply.md new file mode 100644 index 000000000..4a5425f0e --- /dev/null +++ b/.changeset/wet-geese-reply.md @@ -0,0 +1,5 @@ +--- +'@tanstack/form-core': patch +--- + +Fixed `handleSubmit` re-entering while a submission is in flight. Since 1.33.1, the re-submission bypass for `canSubmit` also applied during a pending submission (because `canSubmit` is false while `isSubmitting`), so a repeated `handleSubmit` validated and called `onSubmit` again. In-flight submissions now return early, restoring pre-1.33.1 behavior while keeping the stale-error re-validation on re-submission. diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 4400017d3..5c8986eb2 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -2444,6 +2444,12 @@ export class FormApi< submitMeta ?? (this.options.onSubmitMeta as TSubmitMeta) if (!this.state.canSubmit && !this._devtoolsSubmissionOverride) { + // canSubmit is false while a submission is in flight, so without this + // guard a repeated handleSubmit during a pending onSubmit would fall + // through to the re-submission bypass below and submit the form again. + if (this.baseStore.state.isSubmitting) { + return + } // On re-submission (submissionAttempts > 1), skip the early return so // validateAllFields can re-run and clear stale field errors (e.g. from a // previous onBlur validation that is no longer relevant). The diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index 6ef95f3c5..1a28b93cd 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -2100,6 +2100,73 @@ describe('form api', () => { expect(onSubmitInvalid).toHaveBeenCalledTimes(1) // not called again }) + it('should not call onSubmit again when handleSubmit runs while a submission is in flight', async () => { + // Regression: since the re-submission bypass for canSubmit (1.33.1), a second + // handleSubmit during a pending onSubmit skipped the early return, because + // canSubmit is false while isSubmitting, and submitted the form twice. + // See: https://github.com/TanStack/form/issues/2467 + const pending: Array<() => void> = [] + const onSubmit = vi.fn( + () => new Promise((resolve) => pending.push(resolve)), + ) + + // Note: an earlier test in this file leaks fake timers, so this test opts + // into them explicitly instead of relying on real timers. + vi.useFakeTimers() + + const form = new FormApi({ + defaultValues: { name: 'Ada' }, + onSubmit, + }) + form.mount() + + const first = form.handleSubmit() + await vi.advanceTimersByTimeAsync(10) + expect(form.state.isSubmitting).toBe(true) + + const second = form.handleSubmit() + await vi.advanceTimersByTimeAsync(10) + + expect(onSubmit).toHaveBeenCalledTimes(1) + + pending.forEach((resolve) => resolve()) + await Promise.all([first, second]) + expect(onSubmit).toHaveBeenCalledTimes(1) + vi.useRealTimers() + }) + + it('should still re-validate on re-submission after a completed submit', async () => { + // The in-flight guard must not break the re-submission bypass: once the + // first submission settles, a later handleSubmit still validates and submits. + const pending: Array<() => void> = [] + const onSubmit = vi.fn( + () => new Promise((resolve) => pending.push(resolve)), + ) + + // An earlier test in this file leaks fake timers; this test needs real + // ones so the in-flight promise can settle without manual advancing. + vi.useRealTimers() + + const form = new FormApi({ + defaultValues: { name: 'Ada' }, + onSubmit, + }) + form.mount() + + const first = form.handleSubmit() + await sleep(10) + expect(form.state.isSubmitting).toBe(true) + pending.forEach((resolve) => resolve()) + await first + expect(form.state.isSubmitting).toBe(false) + + const second = form.handleSubmit() + await sleep(10) + expect(onSubmit).toHaveBeenCalledTimes(2) + pending.forEach((resolve) => resolve()) + await second + }) + it('should run all types of async validation on fields during submit', async () => { vi.useFakeTimers()