Skip to content

fix(form-core): canSubmitWhenInvalid not allowing form submission #1632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

814k31
Copy link

@814k31 814k31 commented Jul 25, 2025

No description provided.

@814k31 814k31 changed the title Fix canSubmitWhenInvalid not allowing submit fix: canSubmitWhenInvalid not allowing form submission Jul 25, 2025
@814k31 814k31 force-pushed the bugfix/canSubmitWhenInvalid branch from ffa25a4 to a1f4416 Compare July 25, 2025 07:00
@814k31 814k31 changed the title fix: canSubmitWhenInvalid not allowing form submission fix(form-core): canSubmitWhenInvalid not allowing form submission Jul 25, 2025
@LeCarbonator
Copy link
Contributor

This might be a confusion about what canSubmitWhenInvalid is supposed to do. The purpose is that canSubmit does not trigger an early return and will retry validation before submission. It should not call onSubmit if errors showed up during validation.

Comment on lines 3258 to 3282
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

Copy link

nx-cloud bot commented Jul 28, 2025

View your CI Pipeline Execution ↗ for commit 1fde4b6

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 27s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2025-07-30 13:41:59 UTC

Copy link

pkg-pr-new bot commented Jul 30, 2025

Copy link

codecov bot commented Jul 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.37%. Comparing base (9e9cccd) to head (1fde4b6).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1632      +/-   ##
==========================================
+ Coverage   89.57%   90.37%   +0.79%     
==========================================
  Files          34       36       +2     
  Lines        1497     1621     +124     
  Branches      371      385      +14     
==========================================
+ Hits         1341     1465     +124     
  Misses        139      139              
  Partials       17       17              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants