Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not intended behaviour. However, it may hint at the documentation for canSubmitWhenInvalid needing more information. If you want to change the PR to be that instead, I'm happy to review that instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m happy for you to do that

Ive rejigged our form so I don’t necessarily need this feature now

But I did notice there’s an onSubmitInvalid option

Could also be worth adjusting the new test to make sure that’s called irregardless of the canSubmitWhenInvalid (I could be wrong but I don’t think I saw any tests for that)

im happy to adjust that test on Monday if needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that sounds good!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve reverted the code now and added the test

it would only trigger the onSubmitInvalid callback when the canSubmitWhenInvalid is true so it makes sure that’s works

ill leave it in your court now to add more documentation if you want or any changes to the or you would like

it('should pass the current values to the Standard Schema when calling parseValuesWithSchema', async () => {
const schema = z.object({
firstName: z.string().min(3),
Expand Down