-
-
Notifications
You must be signed in to change notification settings - Fork 465
fix(form-core): touch all fields regardless of canSubmit in <form>.handleSubmit()
#1367
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
fix(form-core): touch all fields regardless of canSubmit in <form>.handleSubmit()
#1367
Conversation
The early return prevents fields from being marked as touched. The check has been removed as field validation prevents final submission anyways.
View your CI Pipeline Execution ↗ for commit 5ee9576.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1367 +/- ##
==========================================
+ Coverage 88.52% 88.57% +0.05%
==========================================
Files 28 28
Lines 1316 1322 +6
Branches 343 345 +2
==========================================
+ Hits 1165 1171 +6
Misses 134 134
Partials 17 17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Looks like you're completely removing the canSubmit check. Instead I think something safer would be to move the validateAllFields line above canSubmit. If we simply remove the canSubmit flag, I don't see the point in having it? validateAllFields is what marks them all as touched. Even then I'm not sure if maintainers would be okay with always allowing validateAllFields to run despite canSubmit being false? |
Indeed. It's the main reason I marked it as draft, as the implemented solution isn't a solution at all. However, some of the test cases are set up, and there's still some questions left up for discussion about how the behaviour should work. I think the main point of the canSubmit flag is that the user can access it in the frontend and, say, disable the submit button. Once handleSubmit is called, the question is what should always happen and what should be conditional. |
Oh! Completely missed this was a draft. My bad! Nice, I understand what you mean. Perhaps some might find it weird if canSubmit is false, yet user CAN technically submit without users of the library explicitly disabling the button. |
…bmit along with test cases to ensure proper form meta, restore the canSubmit check in FormApi#handleSubmit(). Mark all fields as touched before checking canSubmit.
…hub.com/LeCarbonator/tanstack-form into fix-form-validators-canSubmit-behaviour
Along with the test cases, the canSubmit check should now be restored. It simply marks all fields as touched before doing so. That leaves the other questions up for debate, but this is a start. |
Would love to see a fix landed for this - first time using tanstack form and hit this exact issue! Landed on the same solution myself - either removing the "canSubmit" early return, or explicitly touching all fields :) The "standard schema" example shows this bug pretty well, plus oddities with "canSubmit". Type into first name, and you won't see any errors for last name until you focus & blur that field - "Submit" is disabled so you'll only know why it can't be submitted until you click in & out of each field. riewj.movAlong with removing the |
I initially waited because of the quick and dirty fix I implemented and to hear more of the contributors, but since properly implementing it and genuine issues coming up with this bug, I'll mark the PR ready for review. I can always make a future PR if needed :) |
i think its solved with the new |
Not quite. The linked PR addresses validation to still run even if canSubmit is false (using the provided option). This PR addresses that it should always touch all fields, regardless of canSubmit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're saying that clicking the submit button should always touch all fields?
To be honest I don't like it. To me a field is touched only if the user has actually interacted with it, which is not what is happening here.
However, validateAllFields
is already touching everything anyway.
I believe checking on validationAttempts > 0 || isTouched
should be the right condition to show errors without having to touch everything but until we change the isTouched behaviour (I'm not saying it's gonna happen) I believe this PR goes into the right direction making for consistency.
Thank you, great work!
True! However, I see the submit button as "I am sending what I have seen and written your way". If a field is untouched, submitting would therefore imply that the user saw no reason to touch it and is okay with the given default. |
…-validators-canSubmit-behaviour
a637c76
to
67ab191
Compare
…-form-validators-canSubmit-behaviour
67ab191
to
5ee9576
Compare
The early return on this line prevents fields from being marked as touched.
Test cases have been added to address this inconsistency. It's assumed that the following statements must be true:
handleSubmit()
touches all fields, even if itcanSubmit === false
handleSubmit()
does not call form validators if field validators erroredhandleSubmit()
calls form validators if field validators succeededReasoning for the first assumption
validateAllFields
is called duringhandleSubmit
, which touches all fields. However, that code is not reached if validation errored beforehand.This can lead to results where an optional field is edited, causing an
onChange
error in a required field that is not yet touched (a form-level validation schema, for example).The form is now invalid and will not validate all fields, despite no obvious error appearing as no required fields were touched.
This PR closes #1360 if merged.