payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.57k stars 1.5k forks source link

Impossible to create tree similar collection just from create page #5182

Open alekseibahmatov opened 7 months ago

alekseibahmatov commented 7 months ago

Link to reproduction

No response

Describe the Bug

Seems like found a bug in entity creation if collection is related to another entity in the same collection. I'm trying to create categories collection and of course there must be root categories and their children and this chain may be very long.

So I created localized collection where main fields are category_name(text field) and root_category(relationTo field). So when I create root category(that which root_category is empty) everything is ok. Now when I try to create category which root category is the previous category that I just created I get this error:

[18:48:28] ERROR (payload): ValidationError: The following field is invalid: root_category
    at beforeChange (***/node_modules/payload/src/fields/hooks/beforeChange/index.ts:60:11)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async create (***/node_modules/payload/src/collections/operations/create.ts:196:31)
    at async createHandler (/***/node_modules/payload/src/collections/requestHandlers/create.ts:26:17)
Screenshot 2024-02-27 at 19 04 04

But the way to create such entity exists. First we need to create category without root_category and then in edit view assign root category.

Maybe issue in the localization, but I haven't tested this case.

To Reproduce

Collection I'm currently working on

export const Categories: CollectionConfig = {
  slug: "categories",
  labels: {
    singular: {
      en: "Category",
      ee: "Kategooria",
      ru: "Категория",
    },
    plural: {
      en: "Categories",
      ee: "Kategooriad",
      ru: "Категории",
    },
  },
  admin: {
    useAsTitle: "category_name",
    group: {
      en: "Catalog",
      ee: "Kataloog",
      ru: "Каталог",
    }
  },
  fields: [
    {
      name: "category_name",
      type: "text",
      label: {
        en: "Category name",
        ee: "Kategooria nimi",
        ru: "Название категории",
      },
      localized: true,
      required: true,
    },
    {
      name: "root_category",
      type: "relationship",
      relationTo: "categories",
      label: {
        en: "Root category",
        ee: "Vanemkategooria",
        ru: "Родительская категория",
      },
      filterOptions: ({ id }) => {
        return {
          id: { not_equals: id },
        };
      },
      hasMany: false,
    },
    {
      name: "active",
      type: "checkbox",
      label: {
        en: "Activate category",
        ee: "Aktiveeri kategooria",
        ru: "Активировать категорию",
      },
      required: true,
      defaultValue: true,
    },
  ]
};

Payload Version

2.11.1

Adapters and Plugins

db-postgres, bundler-webpack

STR1234 commented 2 months ago

Hey @alekseibahmatov you might want to check out the nested-docs plugin for payload https://payloadcms.com/docs/plugins/nested-docs. Might be a reasonable fit for your use case. Until the issue can be solved.