payloadcms / payloadcms/payload

Array field with the defaultValue gives "The following field is invalid: id" error

Open
#11,871 12 comments 9 reactions 1 assignee View on GitHub

@r1tsuu is already working on this.

Since Mar 28, 2025.

Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

Describe the Bug

I have a collection, let's call it myCollection, with an array-type field named myItems that has a specified defaultValue. Although the field is optional in my case, most of my collection items will have at least four elements in that array. It can be a bit tedious to click "Add item" multiple times, especially since the UI animation takes some time. As a result, I decided to always have four elements in the array field.

However, when trying to quickly add multiple collection items, I encounter the error: "The following field is invalid: id." Additionally, this issue interferes with the Postgres serial IDs, causing the "failed" IDs to be skipped.

Here is the code snippet:

export const MyCollection: CollectionConfig = {
  slug: 'myCollection',
  versions: {
    drafts: true,
    maxPerDoc: 100
  },
  admin: {
    useAsTitle: 'name'
  },
  fields: [
    {
      type: 'tabs',
      tabs: [
        {
          label: 'General',
          fields: [
            {
              name: 'name',
              type: 'text',
              required: true
            },
          ]
        },
        {
          label: 'Content',
          fields: [
            // ...
            {
              name: 'myItems',
              labels: {
                singular: 'item',
                plural: 'items'
              },
              type: 'array',
              required: false,
              defaultValue: Array(4).fill({ name: '', description: '' }),
              fields: [
                {
                  name: 'name',
                  type: 'text',
                  required: true
                },
                {
                  name: 'description',
                  type: 'text',
                  required: true
                }
              ]
            },
          ]
        },
        // ...
      ]
    }
  ]
};

I did some manual investigation and it indeed looks like it tries to submit the same IDs for the array elements.

I tried using defaultValue like this defaultValue: () => Array(4).fill({ name: '', description: '' }), but it didn't work either. It would be great if we could pass-in the callback to generate default value so it doesn't re-use the same object over and over.

Reproduction Steps

See the code snippet above for reference.

  1. Go to myCollection admin page, click Create New.
  2. Type in any name, click Save Draft.
  3. Go Back in browser without refreshing the page.
  4. You get the same "Create New" page, but with all empty fields.
  5. Type in any other name and click save draft - you would get the error.
Which area(s) are affected? (Select all that apply)

Not sure, area: core

Environment Info
Payload: 3.31.0
Node: 22.11.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.