Skip to content

Parent form can stay invalid with empty errors after subform sync using Zod onMount/onChange validators #2069

@marc-niclas

Description

@marc-niclas

Summary

With zod schema validators on both onMount and onChange, a parent form can remain invalid after a nested value is corrected through a separately managed subform.

The key mismatch is:

  • form.state.isValid === false
  • form.state.errors.length === 0
  • form.state.errorMap is empty

At the same time, the tracked parent field still has a stale onMount error in its meta.

Reproduction

StackBlitz repro:
https://stackblitz.com/edit/vitejs-vite-ehbcczvw?file=src%2FApp.tsx

The repro uses:

  • a parent form with person.name
  • a separate child/subform with name
  • zod schemas assigned to both onMount and onChange
  • subform sync via parentForm.setFieldValue('person', formApi.state.values) followed by parentForm.validate('change')

Actual behavior

After typing a valid name in the subform, the parent form shows:

{
  "values": {
    "person": {
      "name": "peter"
    }
  },
  "isValid": false,
  "canSubmit": false,
  "errors": [],
  "errorMap": {}
}

isValid is false, but the form-level errors are empty.

The parent tracked field meta still contains the stale error:

{
  "isTouched": false,
  "isValid": false,
  "errors": [
    {
      "origin": "string",
      "code": "too_small",
      "minimum": 1,
      "inclusive": true,
      "path": [
        "person",
        "name"
      ],
      "message": "Name is required"
    }
  ],
  "errorMap": {
    "onMount": [
      {
        "origin": "string",
        "code": "too_small",
        "minimum": 1,
        "inclusive": true,
        "path": [
          "person",
          "name"
        ],
        "message": "Name is required"
      }
    ]
  }
}

Expected behavior

These should stay in sync:

  • if the form reports errors: [] and errorMap: {}, isValid should be true
  • or, if the field meta is still invalid because of a tracked onMount error, the parent form should reflect that invalid state in its own visible error state

Right now the parent form state and the tracked field meta disagree.

Additional note

I also reproduced the same behavior in a local form-core test using zod schemas and a parent/subform sync pattern. The failing condition is specifically that parent form errors clear while the tracked child field retains a stale onMount error.

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