Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): don't auto-touch shifted siblings on array mutation #2225

Openchatman-media(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/2225/headopened

Showing 3 changed files with 64 additions and 5 deletions.

x
0 of 3 viewed, 3 left
18 additions and 1 deletionpackages/form-core/src/FormApi.tsModified
View file
@@ -1917,7 +1917,24 @@
batch(() => {
fieldsToValidate.forEach((nestedField) => {
fieldValidationPromises.push(
- Promise.resolve().then(() => this.validateField(nestedField, cause)),
+ Promise.resolve().then(() => {
+ // These fields were merely shifted by an array mutation, the user
+ // never interacted with them. Their value did change, so they
+ // still need to be validated, but `validateField` auto-touches as
+ // a side-effect — which would spuriously mark untouched siblings
+ // as touched. Remember the prior touched state and restore it for
+ // fields that weren't touched before.
+ const fieldInstance = this.fieldInfo[nestedField]?.instance
+ const wasTouched = fieldInstance?.store.state.meta.isTouched ?? true
+​
+ const result = this.validateField(nestedField, cause)
+​
+ if (fieldInstance && !wasTouched) {
+ fieldInstance.setMeta((prev) => ({ ...prev, isTouched: false }))
+ }
+​
+ return result
+ }),
Thread on lines 1920 to 1937

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

This still runs the auto-touch side effect through validateField.

Line 1930 still calls validateField, and that method sets isTouched = true for mounted fields before validating on Lines 1967-1971. Restoring false afterward only fixes the final flag; it does not prevent touch-gated side effects that can fire during that window (for example the shouldInvalidateOnMount path later in this file). To fully match the PR objective, shifted mounted fields need a validation path that bypasses the touch mutation entirely, plus a regression test that preserves existing on-mount errors after remove/insert/replace.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/form-core/src/FormApi.ts` around lines 1920 - 1937, The
shifted-field validation in FormApi still goes through validateField, which
auto-touches mounted fields and can trigger touch-gated side effects before the
flag is restored. Update the validation flow used for array-shifted fields in
FormApi so it bypasses the isTouched mutation entirely for fields that were not
previously touched, while still running validation and preserving existing
mounted-field error behavior. Use the fieldInstance / wasTouched logic around
validateField as the entry point, and add a regression test covering
remove/insert/replace cases to ensure on-mount errors remain intact without
spuriously touching siblings.