"Cannot read properties of undefined" when removing array field #1323
Describe the bug#
When removing elements from array using the removeFieldValue api, we sometimes receive:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'errorMap')
I am able to reproduce this when removing an element that contains a field inside that has an error. At some point in the validation logic triggered within removeFieldValue, we're reaching out to get the errorMap for a field that's undefined at that point.
Your minimal, reproducible example#
https://stackblitz.com/edit/vitejs-vite-ypgebmzr?file=src%2Fcomponents%2FForm.tsx
Steps to reproduce#
Simply add a few rows, then delete at least 2. Take a look at the console and you will see:
@tanstack_react-form.js?v=b58a52f4:970 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'errorMap')
at @tanstack_react-form.js?v=b58a52f4:970:27
at functionalUpdate (@tanstack_react-form.js?v=b58a52f4:410:42)
at @tanstack_react-form.js?v=b58a52f4:1132:22
at Store.setState (@tanstack_react-form.js?v=b58a52f4:283:124)
at FormApi.setFieldMeta (@tanstack_react-form.js?v=b58a52f4:1127:22)
at @tanstack_react-form.js?v=b58a52f4:967:20
at batch (@tanstack_react-form.js?v=b58a52f4:255:5)
at FormApi.validateSync (@tanstack_react-form.js?v=b58a52f4:924:7)
at FieldApi.validate (@tanstack_react-form.js?v=b58a52f4:1894:123)
at FormApi.validateField (@tanstack_react-form.js?v=b58a52f4:918:28)
Notice how when we push rows we are pushing a value that is not valid (per the form level schema) to the interestLevel field. If we were to push a valid value (greater than 5) the issue goes away.
Expected behavior#
When I remove an array element, regardless of subfields containing errors or not, we should never be attempting to pull errorMap from undefined (resulting in the error message).
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
React, TypeScript,
TanStack Form adapter#
None
TanStack Form version#
v1.1.2
TypeScript version#
v5.7.2
Additional context#
This will affect any users that attempt to use a standard schema validator at form level and attempt to delete a row with subfields containing errors. I'm looking into fixing it already, please assign me the issue if you have access! Thanks!
I'm having the same issue.
Hope you guys like this approach, it'll allow us to fix the issue without a breaking change.
Long term we may want to update this type though to include the source:
export type ValidationErrorMap< TOnMountReturn = unknown, TOnChangeReturn = unknown, TOnChangeAsyncReturn = unknown, TOnBlurReturn = unknown, TOnBlurAsyncReturn = unknown, TOnSubmitReturn = unknown, TOnSubmitAsyncReturn = unknown, TOnServerReturn = unknown, > = { onMount?: { error: TOnMountReturn source: ValidationSource } onChange?: { error: TOnChangeReturn | TOnChangeAsyncReturn source: ValidationSource } onBlur?: { error: TOnBlurReturn | TOnBlurAsyncReturn source: ValidationSource } onSubmit?: { error: TOnSubmitReturn | TOnSubmitAsyncReturn source: ValidationSource } onServer?: { error: TOnServerReturn source: ValidationSource } }This way we minimize the use of memory and have a single source of truth. Once we get closer to V2 I'm happy to implement that if you like it. For now, we the current implementation of a separate errorSourceMap will work without breaking existing usages.
Balastrong(GitHub profile, opens in a new tab) closed this
I'm using
"@tanstack/react-form": "^1.11.1"and issue still persist.still got this issue in latest version "@tanstack/react-form": "^1.12.3", same reproduce: add several field and remove 2 item
still got this issue in latest version "@tanstack/react-form": "^1.12.3", same reproduce: add several field and remove 2 item
btw it's comes happen cause when we integrate with zod validation, that using method z.array() to create dynamic form. the issue when write extra validation like ".min(1, 'Name is required')". but if we remove the validation, the issues will disappear
const peopleSchemaValidation = z.object({ name: z.string().min(1, "Name is required"), age: z.number(), });
const schemaValidation = z.object({ peoples: z.array(peopleSchemaValidation), });
type FormSchema = z.infer;
const defaultValues: FormSchema = { peoples: [{ name: "", age: 0 }], };
still got this issue in latest version "@tanstack/react-form": "^1.12.3", same reproduce: add several field and remove 2 item

The same happens to me, do we have a proper fix for this, or is this an error with zod integration?
Experiencing a similar issue with tanstack/solid-form v1.23.5 , notably when invoking field().pushValue .
Sign in to join this conversation.
Determined that this issue actually plagues all validations post array shifting/inserting/moving/removing....
I have opened #1324 to resolve this in a much cleaner way.