payload
payload copied to clipboard
plugin-nested-docs: plugin-nested-docs localization issue
Describe the Bug
There are two issues with the Nested Docs Plugin:
1. If I save on another language (which is not my default language), first the parent and then the child, I get the following error:
Unhandled rejection:
ValidationError: The following fields are invalid: title, slug
at beforeChange (/workspaces/payload-cms-template/node_modules/payload/src/fields/hooks/beforeChange/index.ts:55:11)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async updateByID (/workspaces/payload-cms-template/node_modules/payload/src/collections/operations/updateByID.ts:218:16)
[nodemon] app crashed - waiting for file changes before starting...
2. Error Handling in resaveChildren.ts is wrong You need to add a PromiseAll/await for your payload.update function, otherwise the error will never been caught.
try {
children.docs.forEach((child: any) => {
const updateAsDraft =
typeof collection.versions === 'object' &&
collection.versions.drafts &&
child._status !== 'published'
payload.update({
req,
id: child.id,
collection: collection.slug,
draft: updateAsDraft,
data: {
...child,
breadcrumbs: populateBreadcrumbs(req, pluginConfig, collection, child),
},
depth: 0,
locale,
})
})
}
To Reproduce
My project is still running on Payload 1.14.0, but the error should still exist on the latest version. I am only describing the first error here, as I think the second one is self-explanatory. I have the following test collection:
export const CollectionTest: CollectionConfig = {
slug: 'test',
admin: {
useAsTitle: 'title',
defaultColumns: ['title'],
},
versions: {
drafts: true,
},
fields: [
{
name: 'title',
type: 'text',
required: true,
localized: true,
},
{
name: 'slug',
type: 'text',
localized: true,
required: true,
unique: true,
},
createParentField('test', {
admin: {
position: 'sidebar',
},
}),
createBreadcrumbsField('test', {
label: 'Page Breadcrumbs',
}),
],
}
- create a new parent page and a child page (select parent page as parent) in the default language
- change to another language
- go to the parent page and save it. -> Error is thrown and app crashed
Here you find a video, German is my default lang: https://github.com/payloadcms/payload/assets/25500828/b69234e4-8339-4a32-95f1-c26d70cbdb59
Payload Version
1.14.0
Adapters and Plugins
plugin-nested-docs
The same here using:
- "@payloadcms/plugin-nested-docs": "^1.0.8"
- "payload": "^2.0.7"
- and local api payload.create and payload.update changing locale
- PARENT ENTITY: payload.create using a locale
- CHILD ENTITY: payload.create using the same locale and parent field referencing the PARENT ENTITY
- PARENT ENTITY UPDATE: payload.update using another locale
- CHILD ENTITY UPDATE: payload.update using the same locale of the parent update locale and parent field referencing the PARENT ENTITY
App crashes due to uncaught exception and the console output is:
node_modules/payload/src/fields/hooks/beforeChange/index.ts:56
throw new ValidationError(errors, req.t)
^
ValidationError: The following fields are invalid: title, slug, template
at beforeChange (node_modules/payload/src/fields/hooks/beforeChange/index.ts:56:11)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async updateByID (node_modules/payload/src/collections/operations/updateByID.ts:224:18) {
data: [
{ field: 'title', message: 'This field is required.' },
{ field: 'slug', message: 'This field is required.' },
],
isOperational: true,
isPublic: false,
status: 400
}