diff --git a/packages/ui/src/fields/Join/index.tsx b/packages/ui/src/fields/Join/index.tsx index d463f281a2..0eb3d1469d 100644 --- a/packages/ui/src/fields/Join/index.tsx +++ b/packages/ui/src/fields/Join/index.tsx @@ -20,6 +20,7 @@ import { withCondition } from '../../forms/withCondition/index.js' import { useConfig } from '../../providers/Config/index.js' import { useDocumentInfo } from '../../providers/DocumentInfo/index.js' import { FieldDescription } from '../FieldDescription/index.js' +import { FieldError } from '../FieldError/index.js' import { FieldLabel } from '../FieldLabel/index.js' import { fieldBaseClass } from '../index.js' @@ -137,10 +138,13 @@ const JoinFieldComponent: JoinFieldClientComponent = (props) => { const { config, getEntityConfig } = useConfig() - const { customComponents: { AfterInput, BeforeInput, Description, Label } = {}, value } = - useField({ - path, - }) + const { + customComponents: { AfterInput, BeforeInput, Description, Error, Label } = {}, + showError, + value, + } = useField({ + path, + }) const filterOptions: null | Where = useMemo(() => { if (!docID) { @@ -189,9 +193,13 @@ const JoinFieldComponent: JoinFieldClientComponent = (props) => { return (
+ } + /> { + if (data?.enableErrorOnJoin) { + throw new ValidationError({ + collection: 'categories', + errors: [ + { + message: 'enableErrorOnJoin is true', + path: 'joinWithError', + }, + ], + }) + } + }, + ], + }, + }, + { + name: 'enableErrorOnJoin', + type: 'checkbox', + defaultValue: false, + }, ], } diff --git a/test/joins/e2e.spec.ts b/test/joins/e2e.spec.ts index 8eb2f903a3..3299ccecc1 100644 --- a/test/joins/e2e.spec.ts +++ b/test/joins/e2e.spec.ts @@ -18,10 +18,10 @@ import { import { AdminUrlUtil } from '../helpers/adminUrlUtil.js' import { navigateToDoc } from '../helpers/e2e/navigateToDoc.js' import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js' -import { EXPECT_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js' -import { categoriesJoinRestrictedSlug, categoriesSlug, postsSlug, uploadsSlug } from './shared.js' import { reInitializeDB } from '../helpers/reInitializeDB.js' import { RESTClient } from '../helpers/rest.js' +import { EXPECT_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js' +import { categoriesJoinRestrictedSlug, categoriesSlug, postsSlug, uploadsSlug } from './shared.js' const filename = fileURLToPath(import.meta.url) const dirname = path.dirname(filename) @@ -37,7 +37,7 @@ describe('Join Field', () => { let categoriesURL: AdminUrlUtil let uploadsURL: AdminUrlUtil let categoriesJoinRestrictedURL: AdminUrlUtil - let categoryID: string | number + let categoryID: number | string beforeAll(async ({ browser }, testInfo) => { testInfo.setTimeout(TEST_TIMEOUT_LONG) @@ -474,4 +474,13 @@ describe('Join Field', () => { await page.goto(url.admin + '/create-first-user') await expect(page.locator('.field-type.join')).toBeHidden() }) + + test('should render error message when ValidationError is thrown', async () => { + await navigateToDoc(page, categoriesURL) + + await page.locator('#field-enableErrorOnJoin').click() + await page.locator('#action-save').click() + + await expect(page.locator('#field-joinWithError')).toContainText('enableErrorOnJoin is true') + }) }) diff --git a/test/joins/payload-types.ts b/test/joins/payload-types.ts index 315ce77fee..5eee4ad5ea 100644 --- a/test/joins/payload-types.ts +++ b/test/joins/payload-types.ts @@ -51,6 +51,7 @@ export interface Config { localizedPolymorphic: 'posts'; localizedPolymorphics: 'posts'; filtered: 'posts'; + joinWithError: 'posts'; hiddenPosts: 'hidden-posts'; singulars: 'singular'; }; @@ -336,6 +337,11 @@ export interface Category { docs?: (string | Post)[] | null; hasNextPage?: boolean | null; } | null; + joinWithError?: { + docs?: (string | Post)[] | null; + hasNextPage?: boolean | null; + } | null; + enableErrorOnJoin?: boolean | null; updatedAt: string; createdAt: string; } @@ -730,6 +736,8 @@ export interface CategoriesSelect { localizedPolymorphics?: T; singulars?: T; filtered?: T; + joinWithError?: T; + enableErrorOnJoin?: T; updatedAt?: T; createdAt?: T; }