Skip to content

Field Array Validation Not Refreshing Properly When Field is Removed #629

Description

@wscullen

Describe the bug

When multiple fields are added to a field array, and there is validation errors, if you update one of these fields to not have a validation error, and remove a field with a validation error (i - 1 to the good field), the removed field's validation error is applied to the field that does not have validation errors. It seems as though validation (or form state) is not properly re-evaluated and updated when a field is removed from a field array. Due to this bug, a call to form.validateAllFields("change") was attempted as a work around, but this results in the following error:

@tanstack_react-form.js?v=4b8d5915:1011 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'errorMap')
    at @tanstack_react-form…?v=4b8d5915:1011:25
    at functionalUpdate (@tanstack_react-form…s?v=4b8d5915:303:42)
    at @tanstack_react-form…s?v=4b8d5915:753:22
    at Store.setState (@tanstack_react-form…?v=4b8d5915:274:118)
    at FormApi.setFieldMeta (@tanstack_react-form…s?v=4b8d5915:748:18)
    at FieldApi.setMeta (@tanstack_react-form…s?v=4b8d5915:986:43)
    at @tanstack_react-form…?v=4b8d5915:1008:18
    at Store.batch (@tanstack_react-form.js?v=4b8d5915:292:7)
    at FieldApi.validateSync (@tanstack_react-form…s?v=4b8d5915:995:23)
    at FieldApi.validate (@tanstack_react-form…?v=4b8d5915:1106:35)
blitz.810981ba.js:352 

To view this error in the linked example project, uncomment out the indicated line 63 in the Form component.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-81fflp?file=src%2Fcomponents%2FForm.tsx

Steps to reproduce

  1. Add three people fields to the field array
  2. Click Submit to trigger validation
  3. Change the 3rd person's name to a valid value (any string), the validation error for that field will go away
  4. Remove the 2nd person from the field array by clicking the X

Result is the 2nd person (which was the 3rd person) now has the validation error showing of the previously deleted 2nd person.

To see the error, call form.validateAllFields("change") immediately after removing the field from the field array.

Expected behavior

I expect to see all validation for the 2nd person (previously the 3rd person) to continue to not have any errors, instead I'm seeing the previous validation error for the original 2nd person (which was removed).

When trying to work around the issue by manually revalidating all fields with form.validateAllFields("change") after the field is removed, errors are thrown (see above). This error can be avoided if the form.validateAllFields("change") call is placed in a setTimeout.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS 12.7.2
chrome Version 122.0.6261.129 (Official Build) (arm64)

TanStack Form adapter

react-form

TanStack Form version

v0.16.0

TypeScript version

5.2.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions