From a1f44161bacc5e7a0c4fad4d3aa5ad29665fadf4 Mon Sep 17 00:00:00 2001 From: Blake Lovelace Date: Fri, 25 Jul 2025 14:30:28 +0800 Subject: [PATCH 1/4] fix(form-core): canSubmitWhenInvalid not allowing form submission --- packages/form-core/src/FormApi.ts | 4 ++-- packages/form-core/tests/FormApi.spec.ts | 25 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 1dfb98e97..2405a8e9a 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -1780,7 +1780,7 @@ export class FormApi< await this.validateAllFields('submit') - if (!this.state.isFieldsValid) { + if (!this.options.canSubmitWhenInvalid && !this.state.isFieldsValid) { done() this.options.onSubmitInvalid?.({ value: this.state.values, @@ -1792,7 +1792,7 @@ export class FormApi< await this.validate('submit') // Fields are invalid, do not submit - if (!this.state.isValid) { + if (!this.options.canSubmitWhenInvalid && !this.state.isValid) { done() this.options.onSubmitInvalid?.({ value: this.state.values, diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index ebeb17124..4153ce2ca 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -3255,6 +3255,31 @@ describe('form api', () => { expect(form.state.canSubmit).toBe(true) }) + it('should submit, when the form is invalid, with canSubmitWhenInvalid', async () => { + const onSubmitFake = vi.fn(); + const form = new FormApi({ + defaultValues: { + firstName: '', + }, + canSubmitWhenInvalid: true, + validators: { + onMount: () => { + return { + form: 'something went wrong', + fields: { + firstName: 'first name is required', + }, + } + }, + }, + onSubmit: onSubmitFake + }) + form.mount() + expect(form.state.isValid).toBe(false) + await form.handleSubmit() + expect(onSubmitFake).toHaveBeenCalled(); + }) + it('should pass the current values to the Standard Schema when calling parseValuesWithSchema', async () => { const schema = z.object({ firstName: z.string().min(3), From 8b1ed6b253c638a6dba7355029000bbfbe4509d5 Mon Sep 17 00:00:00 2001 From: Blake Lovelace Date: Mon, 28 Jul 2025 10:58:52 +0800 Subject: [PATCH 2/4] revert(form-core): "fix(form-core): canSubmitWhenInvalid not allowing form submission" This reverts commit a1f44161bacc5e7a0c4fad4d3aa5ad29665fadf4. --- packages/form-core/src/FormApi.ts | 4 ++-- packages/form-core/tests/FormApi.spec.ts | 25 ------------------------ 2 files changed, 2 insertions(+), 27 deletions(-) diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 2405a8e9a..1dfb98e97 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -1780,7 +1780,7 @@ export class FormApi< await this.validateAllFields('submit') - if (!this.options.canSubmitWhenInvalid && !this.state.isFieldsValid) { + if (!this.state.isFieldsValid) { done() this.options.onSubmitInvalid?.({ value: this.state.values, @@ -1792,7 +1792,7 @@ export class FormApi< await this.validate('submit') // Fields are invalid, do not submit - if (!this.options.canSubmitWhenInvalid && !this.state.isValid) { + if (!this.state.isValid) { done() this.options.onSubmitInvalid?.({ value: this.state.values, diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index 4153ce2ca..ebeb17124 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -3255,31 +3255,6 @@ describe('form api', () => { expect(form.state.canSubmit).toBe(true) }) - it('should submit, when the form is invalid, with canSubmitWhenInvalid', async () => { - const onSubmitFake = vi.fn(); - const form = new FormApi({ - defaultValues: { - firstName: '', - }, - canSubmitWhenInvalid: true, - validators: { - onMount: () => { - return { - form: 'something went wrong', - fields: { - firstName: 'first name is required', - }, - } - }, - }, - onSubmit: onSubmitFake - }) - form.mount() - expect(form.state.isValid).toBe(false) - await form.handleSubmit() - expect(onSubmitFake).toHaveBeenCalled(); - }) - it('should pass the current values to the Standard Schema when calling parseValuesWithSchema', async () => { const schema = z.object({ firstName: z.string().min(3), From 282208b4070defac18c5b7e5a1f5e5725cfe6f04 Mon Sep 17 00:00:00 2001 From: Blake Lovelace Date: Mon, 28 Jul 2025 11:00:19 +0800 Subject: [PATCH 3/4] test(form-core): Added test to ensure onSubmitIsInvalid is called when canSubmitWhenInvalid is true --- packages/form-core/tests/FormApi.spec.ts | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index ebeb17124..2f1065713 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -3288,6 +3288,31 @@ describe('form api', () => { expect(successResult).toBeUndefined() }) + it('should call onSubmitInvalid, when the form is invalid, with canSubmitWhenInvalid', async () => { + const onSubmitInvalidMock = vi.fn(); + const form = new FormApi({ + defaultValues: { + firstName: '', + }, + canSubmitWhenInvalid: true, + validators: { + onMount: () => { + return { + form: 'something went wrong', + fields: { + firstName: 'first name is required', + }, + } + }, + }, + onSubmitInvalid: onSubmitInvalidMock + }) + form.mount() + expect(form.state.isValid).toBe(false) + await form.handleSubmit() + expect(onSubmitInvalidMock).toHaveBeenCalled(); + }) + it('should pass the current values to the Standard Schema when calling parseValuesWithSchemaAsync', async () => { vi.useFakeTimers() From 1fde4b69f64950772d3fcd688b76c4f8c40e43ad Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 28 Jul 2025 04:55:59 +0000 Subject: [PATCH 4/4] ci: apply automated fixes and generate docs --- packages/form-core/tests/FormApi.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index 2f1065713..aa1903527 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -3289,7 +3289,7 @@ describe('form api', () => { }) it('should call onSubmitInvalid, when the form is invalid, with canSubmitWhenInvalid', async () => { - const onSubmitInvalidMock = vi.fn(); + const onSubmitInvalidMock = vi.fn() const form = new FormApi({ defaultValues: { firstName: '', @@ -3305,12 +3305,12 @@ describe('form api', () => { } }, }, - onSubmitInvalid: onSubmitInvalidMock + onSubmitInvalid: onSubmitInvalidMock, }) form.mount() expect(form.state.isValid).toBe(false) await form.handleSubmit() - expect(onSubmitInvalidMock).toHaveBeenCalled(); + expect(onSubmitInvalidMock).toHaveBeenCalled() }) it('should pass the current values to the Standard Schema when calling parseValuesWithSchemaAsync', async () => {