Array field with the defaultValue gives "The following field is invalid: id" error
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.
- Go to
myCollectionadmin page, clickCreate New. - Type in any
name, click Save Draft. - Go Back in browser without refreshing the page.
- You get the same "Create New" page, but with all empty fields.
- Type in any other
nameand 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
were you able to resolve it?
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.
i also have this issue, ive dropped the database multiple times now to have it fixed. but after a while it just comes back.
I'm encountering it frequently - the only solution is to just refresh the page
Same here
Same
Im experiencing the same issue too.
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.
Issues happen again, I Think this is critical , why no response from the team
Same here
Same here, it is impossible to update the page.
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.