Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mrwebninja opened this issue Mar 15, 2025 · 2 comments
Open

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

mrwebninja opened this issue Mar 15, 2025 · 2 comments
Labels
invalid-reproduction stale status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@mrwebninja
Copy link

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
@mrwebninja mrwebninja added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction Auto-added tag on create to tell bot to check recreation URL, removed after check. labels Mar 15, 2025
@github-actions github-actions bot removed the validate-reproduction Auto-added tag on create to tell bot to check recreation URL, removed after check. label Mar 15, 2025
@rilrom
Copy link
Contributor

rilrom commented Mar 17, 2025

You need to add the draft: true argument to create().

@mrwebninja
Copy link
Author

You need to add the draft: true argument to create().

Thanks

@github-actions github-actions bot added the stale label Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid-reproduction stale status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants