Skip to content
Source
Sign inSign up
TanStack/form

Option for automatically resetting validators #907

Closedoscartbeaumont(GitHub profile, opens in a new tab)wants to merge 8 commits intomain fromrefs/pull/907/headopened

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.
    1. ☁️ Nx Cloud Report

      CI is running/has finished running commands for commit eb01133c815d69e8b2b303f21761fa75c6b5f960. 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

    2. Sent with 💌 from NxCloud.

  • 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

  • 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 155 commits behind head on main.

    Files with missing linesPatch %Lines
    packages/form-core/src/FieldApi.ts50.00%1 Missing :warning:
    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     

    :umbrella: View full report in Codecov by Sentry. :loudspeaker: Have feedback on the report? Share it here.

  • 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!

    packages/form-core/src/FormApi.ts lines 165 to 172
    @@ -160,3 +160,13 @@
    160160 formApi: FormApi<TFormData, TFormValidator>
    161161 }) => void
    162162 transform?: FormTransform<TFormData, TFormValidator>
    163+ /**
    164+ * When true, all previous validation errors will be cleared when running validation.
    165+ *
    166+ * While `true`:
    167+ * - `onMount` and `onBlur` errors will be cleared when `onChange` triggers validation.
    168+ *
    169+ * While `false`:
    170+ * - An `onMount` error will never be cleared.
    171+ * - An `onBlur` error will only be cleared on the next `onBlur` event.
    172+ * - An `onChange` error will only be cleared on the next `onChange` event.

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

    packages/form-core/src/FormApi.ts line 176
    @@ -160,3 +160,17 @@
    160160 formApi: FormApi<TFormData, TFormValidator>
    161161 }) => void
    162162 transform?: FormTransform<TFormData, TFormValidator>
    163+ /**
    164+ * When true, all previous validation errors will be cleared when running validation.
    165+ *
    166+ * While `true`:
    167+ * - `onMount` and `onBlur` errors will be cleared when `onChange` triggers validation.
    168+ *
    169+ * While `false`:
    170+ * - An `onMount` error will never be cleared.
    171+ * - An `onBlur` error will only be cleared on the next `onBlur` event.
    172+ * - An `onChange` error will only be cleared on the next `onChange` event.
    173+ *
    174+ * @default false
    175+ */
    176+ automaticallyResetValidators?: boolean

    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?

  • Superseded by #726 but we can keep discussing in #1005 for improvements in the API

  • Sign in to join this conversation.