Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Data passed to filterOptions sometimes without enriched data from read hooks #10075

Closed
mzronek opened this issue Dec 19, 2024 · 1 comment
Closed

Comments

@mzronek
Copy link

mzronek commented Dec 19, 2024

Describe the Bug

We are filtering a relationship field using filterOptions with data, that is added to a document in an afterRead/beforeRead hook. Sometimes the data argument is passed with the enriched property, sometimes it is not. That results in the field entries not being filtered correctly. In my reproduction I also couldn't save the entry when the undefined filter removed all options from the field.

Link to the code that reproduces this issue

https://github.com/mzronek/payload-bug-filter

Reproduction Steps

Steps:

  1. Create a second user
  2. Create a new Test entry
  3. Save Test entry (beforeRead/afterRead not called before entry is saved)
  4. Select user again and again until the issue comes up

Result: Only shows a single user, although both should be shown.
Console shows: DATA FILTER undefined

To test beforeRead, comment afterRead hook, uncomment beforeRead hook in Test.ts.

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

area: core

Environment Info

Binaries:
  Node: 22.11.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.9.0
  next: 15.1.0
  @payloadcms/db-postgres: 3.9.0
  @payloadcms/email-nodemailer: 3.9.0
  @payloadcms/graphql: 3.9.0
  @payloadcms/next/utilities: 3.9.0
  @payloadcms/payload-cloud: 3.9.0
  @payloadcms/richtext-lexical: 3.9.0
  @payloadcms/translations: 3.9.0
  @payloadcms/ui/shared: 3.9.0
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Home
  Available memory (MB): 65241
  Available CPU cores: 32
@mzronek mzronek added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Dec 19, 2024
@jmikrut
Copy link
Member

jmikrut commented Dec 30, 2024

Hey @mzronek — the filterOptions function is called from a few places - including the admin UI when the form changes.

The admin UI form itself can run without hooks at all, for example, when creating a new document. That means on create, for example, that data added via a hook will never be there.

Instead, you should add your data via a virtual field (virtual: true). You could add a defaultValue as well. This will indeed be calculated prior to filterOptions running everywhere.

Try that and let me know how it goes!

@payloadcms payloadcms locked and limited conversation to collaborators Dec 30, 2024
@jmikrut jmikrut converted this issue into discussion #10255 Dec 30, 2024
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Dec 30, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants