Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): prevent runtime errors when using `deleteField` #1706

Mergedkusiewicz(GitHub profile, opens in a new tab)merged 7 commits intomain fromrefs/pull/1706/headmerged by LeCarbonator(GitHub profile, opens in a new tab)

During implementing this and testing deleteField I found out that this method does not work.

When a form field has onChange validation and the deleteField method is called on it, an error appears.

FormApi.ts:1586 Uncaught TypeError: Cannot read properties of undefined (reading 'errorMap')
    at FormApi.ts:1586:27
    at functionalUpdate (utils.ts:26:8)
    at FormApi.ts:2058:20

error: https://bolt.new/~/vitejs-vite-ceu2fl73

I think flow look like this:

  1. Remove button click => form.deleteField('firstName').
  2. Delete function changes values so onChange validator runs.
  3. This validator runs on the form level and still tries to set errors.fields.email = 'error message' - so validateSync tries to update metadata of deleted field.
  4. setFieldMeta calls functionalUpdate with undefined (since field does not exists in fieldMetaBase).
  5. Updater tries to do ...prev where prev now is undefined -> error.

So validateSync iterates through field list that already semes to be outdated. There is a check if (!fieldMeta) in this function but I guess we have to add as well if (!fieldMetaBase).

    1. View your CI Pipeline Execution ↗ for commit 842956e56a8c3e94d53f7b2721723818251ab6db

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded30sView ↗
      nx run-many --target=build --exclude=examples/**✅ Succeeded1sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-10-07 06:01:31 UTC

    2. More templates

      @tanstack/angular-form

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

      @tanstack/form-core

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

      @tanstack/form-devtools

      npm i https://pkg.pr.new/@tanstack/form-devtools@1706

      @tanstack/lit-form

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

      @tanstack/react-form

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

      @tanstack/react-form-devtools

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

      @tanstack/solid-form

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

      @tanstack/svelte-form

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

      @tanstack/vue-form

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

      commit: 842956e

    3. Codecov Report

      :x: Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review. :white_check_mark: Project coverage is 90.44%. Comparing base (6892ed0) to head (842956e). :warning: Report is 24 commits behind head on main.

      Files with missing linesPatch %Lines
      packages/form-core/src/FormApi.ts75.00%1 Missing :warning:
      Additional details and impacted files
      @@            Coverage Diff             @@
      ##             main    #1706      +/-   ##
      ==========================================
      + Coverage   90.35%   90.44%   +0.09%     
      ==========================================
        Files          38       38              
        Lines        1752     1790      +38     
        Branches      444      453       +9     
      ==========================================
      + Hits         1583     1619      +36     
      - Misses        149      151       +2     
        Partials       20       20              

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

      :rocket: New features to boost your workflow:

      • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
      • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
  • Please, verify the idea. If I'm thinking in a proper direction I will proceed and add unit tests.

  • Will try to review this in the coming days

  • LeCarbonator(GitHub profile, opens in a new tab) approved these changes

    I'm not convinced this would be the ideal solution, but this minor fix helps catch the irritating runtime errors. Thanks for the step-by-step explanation!

  • 🦋 Changeset detected

    Latest commit: 842956e56a8c3e94d53f7b2721723818251ab6db

    The changes in this PR will be included in the next version bump.

    This PR includes changesets to release 9 packages
    NameType
    @tanstack/form-corePatch
    @tanstack/angular-formPatch
    @tanstack/form-devtoolsPatch
    @tanstack/lit-formPatch
    @tanstack/react-formPatch
    @tanstack/solid-formPatch
    @tanstack/svelte-formPatch
    @tanstack/vue-formPatch
    @tanstack/react-form-devtoolsPatch

    Not sure what this means? Click here to learn what changesets are.

    Click here if you're a maintainer who wants to add another changeset to this PR

  • Sign in to join this conversation.