Skip to content

Object properties in array items get set to null when cleared #4952

@filbertteo

Description

@filbertteo

Prerequisites

What theme are you using?

core

Version

6.x

Current Behavior

Validation error is shown when submitting a form for a field when it is being cleared, because it is set to null by ArrayField onChange.

Expected Behavior

No validation error is shown when submitting.

Steps To Reproduce

  1. Visit this playground link
  2. Clear the age field for the 1st array item.
  3. Click submit. Notice that the error message "must be number" appears for the age field.

Environment

- OS:
- Node:
- npm:

Anything else?

Here's my proposed fix in ArrayField.tsx:

  const handleChange = useCallback(
    (value: any, path: FieldPathList, newErrorSchema?: ErrorSchema<T>, id?: string) => {
      const isArrayItemChange = typeof path.at(-1) === "number";
      onChange(
        // We need to treat undefined items as nulls to have validation.
        // See https://github.com/tdegrunt/jsonschema/issues/206
        // Only set to null for array items, and not for object properties within array items.
        isArrayItemChange && value === undefined ? null : value,
        path,
        newErrorSchema as ErrorSchema<T[]>,
        id,
      );
    },
    [onChange],
  );

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions