Skip to content

Conversation

jacobsfletch
Copy link
Member

@jacobsfletch jacobsfletch commented May 21, 2025

It is a common pattern to dynamically show and validate a select field's options based on various criteria such as the current user or underlying document.

Some examples of this might include:

  • Restricting options based on a user's role, e.g. admin-only options
  • Displaying different options based on the value of another field, e.g. a city/state selector

While this is already possible to do with a custom validate function, the user can still view and select the forbidden option...unless you also wired up a custom component.

Now, you can define filterOptions on select fields.

This behaves similarly to the existing filterOptions property on relationship and upload fields, except the return value of this function is simply an array of options, not a query constraint. The result of this function will determine what is shown to the user and what is validated on the server.

Here's an example:

{
  name: 'select',
  type: 'select',
  options: [
    {
      label: 'One',
      value: 'one',
    },
    {
      label: 'Two',
      value: 'two',
    },
    {
      label: 'Three',
      value: 'three',
    },
  ],
  filterOptions: ({ options, data }) =>
    data.disallowOption1
      ? options.filter(
          (option) => (typeof option === 'string' ? options : option.value) !== 'one',
        )
      : options,
}

Copy link
Contributor

@DanRibbens DanRibbens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we married to reduceOptions, I would prefer to just call it filterOptions?

@jacobsfletch jacobsfletch changed the title feat: reduce select fields feat: select field filter options May 22, 2025
@jacobsfletch jacobsfletch merged commit f75d62c into main May 22, 2025
76 checks passed
@jacobsfletch jacobsfletch deleted the feat/reduce-select-options branch May 22, 2025 19:54
jacobsfletch added a commit that referenced this pull request May 27, 2025
You can now specify exactly who can change the constraints within a
query preset.

For example, you want to ensure that only "admins" are allowed to set a
preset to "everyone".

To do this, you can use the new `queryPresets.filterConstraints`
property. When a user lacks the permission to change a constraint, the
option will either be hidden from them or disabled if it is already set.

```ts
import { buildConfig } from 'payload'

const config = buildConfig({
  // ...
  queryPresets: {
    // ...
    filterConstraints: ({ req, options }) =>
      !req.user?.roles?.includes('admin')
        ? options.filter(
            (option) =>
              (typeof option === 'string' ? option : option.value) !==
              'everyone',
          )
        : options,
  },
})
```

The `filterConstraints` functions takes the same arguments as
`reduceOptions` property on select fields introduced in #12487.
Copy link
Contributor

🚀 This is included in version v3.40.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants