Skip to content
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

Option for automatically resetting validators #907

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

Conversation

oscartbeaumont
Copy link
Contributor

@oscartbeaumont oscartbeaumont commented Aug 17, 2024

A potential solution for #903.

I think this is a more general solution than #726 but they both cover the same issue.

My assumption coming into Tanstack Form was that all previous validation errors would be cleared out when running the next validation but that doesn't seem to be the case.

So onMount returning an error means the error will always exist and onBlur returning an error is only cleared on the next blur event.

New behavior

When form.options.automaticallyResetValidators === true previous validation errors are cleared when running the validator,
else retain the previous behaviour.

Known problems

Calling .validateSync or .validateAsync will not cause the fields to be reset. These fields are however marked as @private so I think this is fine.

I think fixing this would also add a lot more complexity because the .validate method calls .validateSync and then .validateAsync, so we would need to know not to clear the errors that were just added by .validateSync in .validateAsync.

Questions

  • One major question I have is what should the default be? Personally I think it should be true but this would be a breaking change.

Copy link

nx-cloud bot commented Aug 17, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit eb01133. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link

pkg-pr-new bot commented Aug 17, 2024

commit: eb01133

@tanstack/angular-form

pnpm add https://pkg.pr.new/@tanstack/angular-form@907

@tanstack/form-core

pnpm add https://pkg.pr.new/@tanstack/form-core@907

@tanstack/lit-form

pnpm add https://pkg.pr.new/@tanstack/lit-form@907

@tanstack/react-form

pnpm add https://pkg.pr.new/@tanstack/react-form@907

@tanstack/solid-form

pnpm add https://pkg.pr.new/@tanstack/solid-form@907

@tanstack/valibot-form-adapter

pnpm add https://pkg.pr.new/@tanstack/valibot-form-adapter@907

@tanstack/vue-form

pnpm add https://pkg.pr.new/@tanstack/vue-form@907

@tanstack/yup-form-adapter

pnpm add https://pkg.pr.new/@tanstack/yup-form-adapter@907

@tanstack/zod-form-adapter

pnpm add https://pkg.pr.new/@tanstack/zod-form-adapter@907

Open in Stackblitz

More templates

@oscartbeaumont oscartbeaumont marked this pull request as ready for review August 17, 2024 03:53
@oscartbeaumont oscartbeaumont marked this pull request as draft August 17, 2024 03:57
Copy link

codecov bot commented Aug 17, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 88.48%. Comparing base (5473bb8) to head (eb01133).
Report is 91 commits behind head on main.

Files Patch % Lines
packages/form-core/src/FieldApi.ts 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #907      +/-   ##
==========================================
- Coverage   91.55%   88.48%   -3.07%     
==========================================
  Files          21       26       +5     
  Lines         900      938      +38     
  Branches      206      210       +4     
==========================================
+ Hits          824      830       +6     
- Misses         71      102      +31     
- Partials        5        6       +1     

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

@oscartbeaumont oscartbeaumont marked this pull request as ready for review August 17, 2024 04:37
Copy link
Member

@Balastrong Balastrong left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, the overall idea looks good!

To answer the question of what the default behaviour should be, I think it makes more sense to keep this false for primarily two reasons:

  1. As you also mentioned, that would be a breaking change
  2. This adds extra complexity on setting validators, most notably every time you trigger blur on an input it will remove the onChange errors (if a blur validator is set) which might not be expected.

This last point is also the main reason why my initial idea was to only focus on the mount errors.

Would you mind adding some unit tests? I'll be happy to merge this!

Comment on lines +165 to +172
*
* While `true`:
* - `onMount` and `onBlur` errors will be cleared when `onChange` triggers validation.
*
* While `false`:
* - An `onMount` error will never be cleared.
* - An `onBlur` error will only be cleared on the next `onBlur` event.
* - An `onChange` error will only be cleared on the next `onChange` event.
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably add an extra line on top like "Some examples:" since those aren't hardcoded rules.

*
* @default false
*/
automaticallyResetValidators?: boolean
Copy link
Member

Choose a reason for hiding this comment

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

The action this flag enables is that errors are removed when a new validator is run, I think we should better explain this.

I like resetOnRevalidate to indicate that an action (reset) is performed when something happens (onRevalidate) or resetErrorsOnRevalidate if we want to be extra explicit.

What do you think?

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