FormGroup validator returning sub-field errors for unmounted fields throws "Cannot read properties of undefined (reading 'errorMap')" #2231
Describe the bug#
When a FormGroup validator returns errors for sub-fields using the documented { fields } shape (see the Form Groups guide: "Can set errors on sub-fields"), and one of those sub-fields is not mounted as a field yet (i.e. there is no <form.Field> for it, so it has no entry in fieldMetaBase), group validation throws:
TypeError: Cannot read properties of undefined (reading 'errorMap')
at FormGroupApi.js:242
at functionalUpdate (utils.js:4)
at FormApi.setFieldMeta (FormApi.js:618)
at FormGroupApi.distributeFieldErrors (FormGroupApi.js:239)
at validateFieldOrGroupFn (FormGroupApi.js:330)
distributeFieldErrors intentionally continues when the child field has no meta but the validator produced an error for it:
if (!fieldMeta && !newFormValidatorError) continue
However, it then calls setFieldMeta with an updater that unconditionally dereferences the previous meta:
this.form.setFieldMeta(fullName, (prev) => ({
...prev,
errorMap: {
...prev.errorMap, // prev is undefined -> TypeError
},
}))
This is a real-world pattern for multi-step wizards: each step is a FormGroup validated by a step-level schema. Standard Schema validators (e.g. Zod) reach the same code path via remapStandardSchemaResultForGroup, while fields are rendered progressively. As a result, the schema can legitimately report errors for paths that do not yet have mounted fields.
This is the same bug class that was previously fixed for FormApi's field-error fan-out in:
Those fixes used getFieldMeta(field) ?? defaultFieldMeta on reads and (prev = defaultFieldMeta) => for updater writes. FormGroupApi.distributeFieldErrors is the remaining unguarded occurrence.
Your minimal, reproducible example#
https://stackblitz.com/edit/vite-react-ts-xewyjxed
Steps to reproduce#
npm install
# headless repro: prints "BUG REPRODUCED" plus the TypeError stack
npm run repro:core
# browser repro
npm run dev
For the browser repro: open the app, type one character into the "First name" input, and check the console for the TypeError.
Expected behavior#
As a user, I expected the group validator's fields errors to be stored for not-yet-mounted sub-fields (creating their meta from defaults, as FormApi's form-level validators do since #1706/#2056), so the error shows up as soon as the field mounts. Instead, the whole form crashes.
How often does this bug happen?#
Every time
Screenshots or Videos#
https://github.com/user-attachments/assets/2c6f838d-6e0a-489e-a69d-beed13d2943c
Platform#
- OS: macOS
- Browser: Chrome (reproduces in any browser; also reproduces in plain Node with @tanstack/form-core only, no browser/DOM - see repro-core.mjs in the linked repro)
- Version: n/a
TanStack Form adapter#
react-form
TanStack Form version#
1.33.0
TypeScript version#
5.8.3
Additional context#
Not a duplicate of #1709/#845/#1336. Those issues covered FormApi's and FieldApi's error fan-out and have already been fixed. This is the same unguarded pattern in FormGroupApi.distributeFieldErrors, which none of the prior fixes (#1706, #2056) touched.
crutchcorn(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.