Newly pushed array fields do not show form-level errors on submit while existing errors remain

オープン
#2,129 コメント 2 件 リアクション 3 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@nikhilsdabhade がすでに取り組んでいます。

2026年6月4日 から。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
静か
技術スタック
react, typescript
領域
frontend

調査の方向性

packages/form-core/src/FieldApi.ts から始め、FieldApi.validateSync と、フィールドレベルのバリデーターが存在しない場合のバリデーションループに注目してください。issue にある submit、add-person、submit のシーケンスを再現し、提供されている失敗するテストケースを使って、フォームレベルのエラーが新しいフィールドに到達することを確認してください。2 回目の submit の後に新しい配列フィールドに required エラーが表示されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

v1 v2: needs investigation
Describe the bug

When using a form-level validator, if the form already has an error and a new array field is added with pushValue, submitting again does not show the new field’s error even though it is invalid.

This happens both with and without canSubmitWhenInvalid. Although, for different reasons I think.

This reproduces with the same submit-driven sequence when the form-level validator is configured as onSubmit and also with e.g. onChange or onBlur.

With onChange/onBlur the missing error will appear when a change/blur happens anywhere in the form.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-e4ageewt?file=src%2FApp.tsx

Steps to reproduce
  1. Submit empty form.
  2. Click Add person.
  3. Submit again.
  4. The new person field does not show Name is required.
Expected behavior

After the second submit, all invalid fields should show errors, including the newly added array field.

How often does this bug happen?

Every time

Screenshots or Videos

https://github.com/user-attachments/assets/f667411d-2f92-4b3d-b37f-cb6191288957

Platform
  • OS: Linux
  • Browser: Chrome, Firefox
  • Version: latest
TanStack Form adapter

react-form

TanStack Form version

1.29.0

TypeScript version

6.0.2

Additional context

Possible cause: for a newly mounted array field with no field-level validator for the active cause, FieldApi.validateSync(cause, errorFromForm) gets an empty validator array. Because the per-field validation loop never runs, the field never reconciles errorFromForm into meta.errorMap. So the form-level validator can report the error, but the new field does not pick it up until some later validation event causes that field state to reconcile.

Here is a small failing test case:

  it('should apply form-level submit errors even when the field has no field-level submit validators', () => {
    const form = new FormApi({
      defaultValues: {
        name: '',
      },
    })

    form.mount()

    const field = new FieldApi({
      form,
      name: 'name',
      defaultMeta: {
        isTouched: true,
      },
    })

    field.mount()

    field.validateSync('submit', {
      onSubmit: 'Name is required',
    })

    expect(field.getMeta().errorMap.onSubmit).toBe('Name is required')
    expect(field.getMeta().errors).toContain('Name is required')
  })

Small hack to make it work as expected:

diff --git a/packages/form-core/src/FieldApi.ts b/packages/form-core/src/FieldApi.ts
--- a/packages/form-core/src/FieldApi.ts
+++ b/packages/form-core/src/FieldApi.ts
@@ -1679,6 +1679,10 @@ export class FieldApi<
       validationLogic:
         this.form.options.validationLogic || defaultValidationLogic,
     })
+
+    const validatesToRun = validates.length
+      ? validates
+      : ([{ cause, validate: undefined }] as typeof validates)
 
     const linkedFields = this.getLinkedFields(cause)
     const linkedFieldValidates = linkedFields.reduce(
@@ -1752,7 +1756,7 @@ export class FieldApi<
         }
       }
 
-      for (const validateObj of validates) {
+      for (const validateObj of validatesToRun) {
         validateFieldFn(this, validateObj)
       }
       for (const fieldValitateObj of linkedFieldValidates) {

With canSubmitWhenInvalid: false I'm guessing it happens due to canSubmit blocking? Maybe that is intended? I think I would still expect validation to run.

主要言語
TypeScript
スター
6.8k
フォーク
705
平均マージ
1日 7時間
マージ済み PR(30日)
14

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/form のほかの issue

TanStack/form の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。