Skip to content

Commit fdebc84

Browse files
authored
fix: sanitize virtual fields in admin.useAsTitle (#8620)
Fixes #8525. Disallows to set `admin.useAsTitle` with a virtual field. Updates docs / jsdoc accordingly
1 parent f6acfdb commit fdebc84

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

docs/admin/collections.mdx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ export const MyCollection: CollectionConfig = {
2525

2626
The following options are available:
2727

28-
| Option | Description |
29-
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
30-
| **`group`** | Text used as a label for grouping Collection and Global links together in the navigation. |
31-
| **`hidden`** | Set to true or a function, called with the current user, returning true to exclude this Collection from navigation and admin routing. |
32-
| **`hooks`** | Admin-specific hooks for this Collection. [More details](../hooks/collections). |
33-
| **`useAsTitle`** | Specify a top-level field to use for a document title throughout the Admin Panel. If no field is defined, the ID of the document is used as the title. |
28+
| Option | Description |
29+
| -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
30+
| **`group`** | Text used as a label for grouping Collection and Global links together in the navigation. |
31+
| **`hidden`** | Set to true or a function, called with the current user, returning true to exclude this Collection from navigation and admin routing. |
32+
| **`hooks`** | Admin-specific hooks for this Collection. [More details](../hooks/collections). |
33+
| **`useAsTitle`** | Specify a top-level field to use for a document title throughout the Admin Panel. If no field is defined, the ID of the document is used as the title. A field with `virtual: true` cannot be used as the title. |
3434
| **`description`** | Text to display below the Collection label in the List View to give editors more information. Alternatively, you can use the `admin.components.Description` to render a React component. [More details](#components). |
35-
| **`defaultColumns`** | Array of field names that correspond to which columns to show by default in this Collection's List View. |
36-
| **`hideAPIURL`** | Hides the "API URL" meta field while editing documents within this Collection. |
37-
| **`enableRichTextLink`** | The [Rich Text](../fields/rich-text) field features a `Link` element which allows for users to automatically reference related documents within their rich text. Set to `true` by default. |
38-
| **`enableRichTextRelationship`** | The [Rich Text](../fields/rich-text) field features a `Relationship` element which allows for users to automatically reference related documents within their rich text. Set to `true` by default. |
39-
| **`meta`** | Page metadata overrides to apply to this Collection within the Admin Panel. [More details](./metadata). |
40-
| **`preview`** | Function to generate preview URLs within the Admin Panel that can point to your app. [More details](#preview). |
41-
| **`livePreview`** | Enable real-time editing for instant visual feedback of your front-end application. [More details](../live-preview/overview). |
42-
| **`components`** | Swap in your own React components to be used within this Collection. [More details](#components). |
43-
| **`listSearchableFields`** | Specify which fields should be searched in the List search view. [More details](#list-searchable-fields). |
44-
| **`pagination`** | Set pagination-specific options for this Collection. [More details](#pagination). |
35+
| **`defaultColumns`** | Array of field names that correspond to which columns to show by default in this Collection's List View. |
36+
| **`hideAPIURL`** | Hides the "API URL" meta field while editing documents within this Collection. |
37+
| **`enableRichTextLink`** | The [Rich Text](../fields/rich-text) field features a `Link` element which allows for users to automatically reference related documents within their rich text. Set to `true` by default. |
38+
| **`enableRichTextRelationship`** | The [Rich Text](../fields/rich-text) field features a `Relationship` element which allows for users to automatically reference related documents within their rich text. Set to `true` by default. |
39+
| **`meta`** | Page metadata overrides to apply to this Collection within the Admin Panel. [More details](./metadata). |
40+
| **`preview`** | Function to generate preview URLs within the Admin Panel that can point to your app. [More details](#preview). |
41+
| **`livePreview`** | Enable real-time editing for instant visual feedback of your front-end application. [More details](../live-preview/overview). |
42+
| **`components`** | Swap in your own React components to be used within this Collection. [More details](#components). |
43+
| **`listSearchableFields`** | Specify which fields should be searched in the List search view. [More details](#list-searchable-fields). |
44+
| **`pagination`** | Set pagination-specific options for this Collection. [More details](#pagination). |
4545

4646
### Components
4747

packages/payload/src/collections/config/useAsTitle.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CollectionConfig } from '../../index.js'
22

33
import { InvalidConfiguration } from '../../errors/InvalidConfiguration.js'
4-
import { fieldAffectsData } from '../../fields/config/types.js'
4+
import { fieldAffectsData, fieldIsVirtual } from '../../fields/config/types.js'
55
import flattenFields from '../../utilities/flattenTopLevelFields.js'
66

77
/**
@@ -33,6 +33,11 @@ export const validateUseAsTitle = (config: CollectionConfig) => {
3333
}
3434
}
3535
} else {
36+
if (useAsTitleField && fieldIsVirtual(useAsTitleField)) {
37+
throw new InvalidConfiguration(
38+
`The field "${config.admin.useAsTitle}" specified in "admin.useAsTitle" in the collection "${config.slug}" is virtual. A virtual field cannot be used as the title.`,
39+
)
40+
}
3641
if (!useAsTitleField) {
3742
throw new InvalidConfiguration(
3843
`The field "${config.admin.useAsTitle}" specified in "admin.useAsTitle" does not exist in the collection "${config.slug}"`,

packages/payload/src/fields/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ export interface FieldBase {
424424
/**
425425
* Pass `true` to disable field in the DB
426426
* for [Virtual Fields](https://payloadcms.com/blog/learn-how-virtual-fields-can-help-solve-common-cms-challenges):
427+
* A virtual field cannot be used in `admin.useAsTitle`
427428
*/
428429
virtual?: boolean
429430
}

0 commit comments

Comments
 (0)