payload icon indicating copy to clipboard operation
payload copied to clipboard

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

Open simeiko opened this issue 1 year ago • 12 comments

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

simeiko avatar Mar 26 '25 02:03 simeiko

were you able to resolve it?

omaroubari avatar May 15 '25 20:05 omaroubari

were you able to resolve it?

As a workaround, I'm performing a full page refresh (F5), which works fine for now. However, a proper fix would still be appreciated.

simeiko avatar May 17 '25 19:05 simeiko

i also have this issue, ive dropped the database multiple times now to have it fixed. but after a while it just comes back.

androbro avatar Jun 16 '25 08:06 androbro

I'm encountering it frequently - the only solution is to just refresh the page

MacPiston avatar Aug 29 '25 08:08 MacPiston

Same here

alexanderkalachev avatar Sep 10 '25 17:09 alexanderkalachev

Same

leosequani avatar Dec 09 '25 21:12 leosequani

Im experiencing the same issue too.

georgeyasbic avatar Dec 11 '25 09:12 georgeyasbic

I'm encountering the same issue across multiple collections. In some cases, removing orphaned records resolves the problem, but in other cases the issue persists and the system remains stuck. We need a consistent and permanent solution rather than relying on temporary workarounds.

ghost avatar Dec 17 '25 09:12 ghost

Issues happen again, I Think this is critical , why no response from the team

anhdd-kuro avatar Dec 19 '25 01:12 anhdd-kuro

Same here

jstarstech avatar Dec 22 '25 13:12 jstarstech

Same here, it is impossible to update the page.

adrianoresende avatar Jan 11 '26 21:01 adrianoresende

I fixed it by deleting all versions on the affected block. i.e., The error is with a block, ForWhom, so I deleted all versions on _pages_v_blocks_for_whom and deleted some relation parent that doesn't exist anymore on pages_blocks_for_whom_{array}_list where array is a list inside of block.

adrianoresende avatar Jan 11 '26 22:01 adrianoresende