Skip to content

fix(form-core): onSubmitInvalid not called when canSubmit false #1697

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

Pascalmh
Copy link
Contributor

onSubmitInvalid callback in FormApi is called when a submission attempt is made while canSubmit is false

Fixes #1696

Copy link

nx-cloud bot commented Aug 15, 2025

View your CI Pipeline Execution ↗ for commit 15705d6

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

☁️ Nx Cloud last updated this comment at 2025-08-22 07:42:28 UTC

Copy link

pkg-pr-new bot commented Aug 15, 2025

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1697

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1697

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1697

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1697

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1697

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@1697

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1697

commit: 15705d6

Copy link

codecov bot commented Aug 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.53%. Comparing base (424c3c7) to head (15705d6).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1697      +/-   ##
==========================================
+ Coverage   90.52%   90.53%   +0.01%     
==========================================
  Files          37       37              
  Lines        1688     1690       +2     
  Branches      421      421              
==========================================
+ Hits         1528     1530       +2     
  Misses        143      143              
  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.

formApi: this,
meta: submitMetaArg,
})
return
Copy link
Contributor

Choose a reason for hiding this comment

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

While this ensures onSubmitInvalid gets called, it happens before the submit validation. This means the formApi will have a stale error state when the callback is triggered.

@LeCarbonator
Copy link
Contributor

@kusiewicz It happens before the submit validation, but only in cases where canSubmit is false. That means there are currently errors present that have not been resolved, which is not stale.

If a user wanted to rerun validators even if canSubmit is false, they can use the canSubmitWhenInvalid property

@kusiewicz
Copy link
Contributor

kusiewicz commented Aug 16, 2025

@kusiewicz It happens before the submit validation, but only in cases where canSubmit is false. That means there are currently errors present that have not been resolved, which is not stale.

If a user wanted to rerun validators even if canSubmit is false, they can use the canSubmitWhenInvalid property

Ok i got it. I just thought that there:

  onSubmitInvalid: ({ formApi }) => {
    console.log(formApi.state.fieldMeta.xxx.errors);
  }

we should expect most recent errors, didn't know about canSubmitWhenInvalid

@LeCarbonator
Copy link
Contributor

well, they are the most recent errors. The most recent errors are preventing a submit altogether, so this PR asserts that attempting one should still trigger onSubmitInvalid

@jiji-hoon96
Copy link

Hello @Pascalmh!

I encountered the same issue mentioned by @kusiewicz. Calling onSubmitInvalid before performing validation may result in outdated error states being passed to the callback.

Would you consider restructuring the logic to first perform the validation, check canSubmit, and then call onSubmitInvalid with the updated state? In this case, the user would receive the most recent validation error in the callback.

If it's okay with you, would it be possible for me to work on this and submit a PR?

@Pascalmh
Copy link
Contributor Author

@jiji-hoon96 sure, if you want you can PR against my fork and I'll merge your changes in so they appear in this PR here - or you can create a new PR, whatever you prefere 👍

@LeCarbonator
Copy link
Contributor

@jiji-hoon96 Are you sure it's stale? Even with canSubmitWhenInvalid: true?

@jiji-hoon96
Copy link

Yes. I have confirmed its validity through testing. I may have made a mistake, so I would appreciate it if you could take a look at #1711 PR.

@jiji-hoon96
Copy link

Pascalmh#5

I have submitted a pull request with some modifications to @Pascalmh`s work.

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.

form.onSubmitInvalid not called when canSubmit is false
4 participants