Skip to content

Local API:Required Field Validation Error When Saving Drafts #11723

Open
@mrwebninja

Description

@mrwebninja

Describe the Bug

I am working on a Payload CMS app and have a specific requirement: when creating a document in one collection, another document should be created as a draft in a different collection.

Example: When an order is created, an application form should be created as a draft in the awardApplications collection, using some fields from the created order.

To achieve this, I am using the afterChange hook in the orders collection configuration. Here is the code for the hook:

 afterChange: [
      async ({ doc, req, operation }) => {
        if (operation === 'create') {
          const payload = req.payload
          const userId = doc.user
          const orderId = doc.id

          for (const item of doc.orderItems) {
            const productId = item.productId
            const regionId = item.region

            await payload.create({
              collection: 'awardApplications',
              data: {
                user: userId,
                product: productId,
                region: regionId,
                order: orderId,
                _status: 'draft',
              },
            })
          }
        }
      },
    ],

but error on awardApplications
The following fields are invalid: First Name, Last Name, Email, Phone........

This behavior seems inconsistent with the Payload CMS documentation on drafts
https://payloadcms.com/docs/versions/drafts#draft-api
If draft is enabled while creating or updating a document, all fields are considered as not required, so that you can save drafts that are incomplete.

I am saving the document with the _status set to 'draft', so it should not trigger validation errors for required fields.

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/tree/main/templates/website

Reproduction Steps

If I set all fields in the awardApplications configuration to required: false, I can successfully save the document in awardApplications.

Which area(s) are affected? (Select all that apply)

Not sure

Environment Info

Node: 22.14.0
Relevant Packages:
  payload: 3.28.1
  next: 15.2.0
  @payloadcms/db-mongodb: 3.25.0
  @payloadcms/email-nodemailer: 3.27.0
  @payloadcms/email-resend: 3.25.0
  @payloadcms/graphql: 3.27.0
  @payloadcms/live-preview: 3.27.0
  @payloadcms/live-preview-react: 3.25.0
  @payloadcms/next/utilities: 3.25.0
  @payloadcms/payload-cloud: 3.25.0
  @payloadcms/plugin-form-builder: 3.25.0
  @payloadcms/plugin-nested-docs: 3.25.0
  @payloadcms/plugin-redirects: 3.25.0
  @payloadcms/plugin-search: 3.25.0
  @payloadcms/plugin-seo: 3.25.0
  @payloadcms/richtext-lexical: 3.25.0
  @payloadcms/translations: 3.27.0
  @payloadcms/ui/shared: 3.25.0
  react: 19.0.0
  react-dom: 19.0.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions