Skip to content

Commit a1f4416

Browse files
committed
fix(form-core): canSubmitWhenInvalid not allowing form submission
1 parent 1089876 commit a1f4416

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/form-core/src/FormApi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ export class FormApi<
17801780

17811781
await this.validateAllFields('submit')
17821782

1783-
if (!this.state.isFieldsValid) {
1783+
if (!this.options.canSubmitWhenInvalid && !this.state.isFieldsValid) {
17841784
done()
17851785
this.options.onSubmitInvalid?.({
17861786
value: this.state.values,
@@ -1792,7 +1792,7 @@ export class FormApi<
17921792
await this.validate('submit')
17931793

17941794
// Fields are invalid, do not submit
1795-
if (!this.state.isValid) {
1795+
if (!this.options.canSubmitWhenInvalid && !this.state.isValid) {
17961796
done()
17971797
this.options.onSubmitInvalid?.({
17981798
value: this.state.values,

packages/form-core/tests/FormApi.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,31 @@ describe('form api', () => {
32553255
expect(form.state.canSubmit).toBe(true)
32563256
})
32573257

3258+
it('should submit, when the form is invalid, with canSubmitWhenInvalid', async () => {
3259+
const onSubmitFake = vi.fn();
3260+
const form = new FormApi({
3261+
defaultValues: {
3262+
firstName: '',
3263+
},
3264+
canSubmitWhenInvalid: true,
3265+
validators: {
3266+
onMount: () => {
3267+
return {
3268+
form: 'something went wrong',
3269+
fields: {
3270+
firstName: 'first name is required',
3271+
},
3272+
}
3273+
},
3274+
},
3275+
onSubmit: onSubmitFake
3276+
})
3277+
form.mount()
3278+
expect(form.state.isValid).toBe(false)
3279+
await form.handleSubmit()
3280+
expect(onSubmitFake).toHaveBeenCalled();
3281+
})
3282+
32583283
it('should pass the current values to the Standard Schema when calling parseValuesWithSchema', async () => {
32593284
const schema = z.object({
32603285
firstName: z.string().min(3),

0 commit comments

Comments
 (0)