Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-submit-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/form-core': patch
---

Prevent concurrent form submissions while an async submit is already in progress.
2 changes: 2 additions & 0 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,8 @@ export class FormApi<
* Handles the form submission, performs validation, and calls the appropriate onSubmit or onSubmitInvalid callbacks.
*/
_handleSubmit = async (submitMeta?: TSubmitMeta): Promise<void> => {
if (this.state.isSubmitting) return

this.baseStore.setState((old) => ({
...old,
// Submission attempts mark the form as not submitted
Expand Down
32 changes: 32 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3521,6 +3521,38 @@ describe('form api', () => {
expect(form.state.isSubmitSuccessful).toBe(false)
})

it('should not call onSubmit again while a submission is in progress', async () => {
vi.useFakeTimers()
try {
const submitDelayMs = 1000
const expectedSubmitCount = 1
const onSubmit = vi.fn(async () => {
await sleep(submitDelayMs)
})
const form = new FormApi({
defaultValues: {
name: 'test',
},
onSubmit,
})

form.mount()

const firstSubmit = form.handleSubmit()
await vi.advanceTimersByTimeAsync(0)

const secondSubmit = form.handleSubmit()
await vi.advanceTimersByTimeAsync(0)

expect(onSubmit).toHaveBeenCalledTimes(expectedSubmitCount)

await vi.runAllTimersAsync()
await Promise.all([firstSubmit, secondSubmit])
} finally {
vi.useRealTimers()
}
})

it('should reset the fields value and meta to default state', async () => {
const form = new FormApi({
defaultValues: {
Expand Down