Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): create complete field meta in setFieldValue for unmounted fields #2433

Openmaricastroc(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/2433/headopened

Showing 4 changed files with 64 additions and 3 deletions (loading 1).

x
0 of 4 viewed, 4 left
5 additions and 0 deletions.changeset/complete-unmounted-field-meta.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/form-core': patch
+---
+​
+Fix `form.setFieldValue()` creating incomplete metadata for a field that is not mounted yet. A `FormGroup` validator that later reports an error for that field no longer throws `TypeError: Cannot read properties of undefined (reading 'onChange')`, and the error is assigned to the field.
2 additions and 3 deletionspackages/form-core/src/FormApi.tsModified
View file
@@ -2659,13 +2659,12 @@
batch(() => {
if (!dontUpdateMeta) {
- this.setFieldMeta(field, (prev) => ({
+ this.setFieldMeta(field, (prev = defaultFieldMeta) => ({
...prev,
isTouched: true,
isDirty: true,
errorMap: {
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
- ...prev?.errorMap,
+ ...prev.errorMap,
onMount: undefined,
57 additions and 0 deletionspackages/form-core/tests/FormApi.spec.tsModified
View file
@@ -279,6 +279,63 @@
expect(form.state.isPristine).toBe(false)