diff --git a/templates/website/src/collections/Posts/hooks/populateAuthors.ts b/templates/website/src/collections/Posts/hooks/populateAuthors.ts index 7ddb6220a..40b4fe5b4 100644 --- a/templates/website/src/collections/Posts/hooks/populateAuthors.ts +++ b/templates/website/src/collections/Posts/hooks/populateAuthors.ts @@ -6,26 +6,31 @@ import { User } from 'src/payload-types' // GraphQL will not return mutated user data that differs from the underlying schema // So we use an alternative `populatedAuthors` field to populate the user data, hidden from the admin UI export const populateAuthors: CollectionAfterReadHook = async ({ doc, req, req: { payload } }) => { - if (doc?.authors) { + if (doc?.authors && doc?.authors?.length > 0) { const authorDocs: User[] = [] for (const author of doc.authors) { - const authorDoc = await payload.findByID({ - id: typeof author === 'object' ? author?.id : author, - collection: 'users', - depth: 0, - req, - }) + try { + const authorDoc = await payload.findByID({ + id: typeof author === 'object' ? author?.id : author, + collection: 'users', + depth: 0, + }) - if (authorDoc) { - authorDocs.push(authorDoc) + if (authorDoc) { + authorDocs.push(authorDoc) + } + + if (authorDocs.length > 0) { + doc.populatedAuthors = authorDocs.map((authorDoc) => ({ + id: authorDoc.id, + name: authorDoc.name, + })) + } + } catch { + // swallow error } } - - doc.populatedAuthors = authorDocs.map((authorDoc) => ({ - id: authorDoc.id, - name: authorDoc.name, - })) } return doc diff --git a/templates/website/src/payload-types.ts b/templates/website/src/payload-types.ts index 4814fe771..1dfe387bc 100644 --- a/templates/website/src/payload-types.ts +++ b/templates/website/src/payload-types.ts @@ -1700,4 +1700,4 @@ export interface Auth { declare module 'payload' { export interface GeneratedTypes extends Config {} -} +} \ No newline at end of file diff --git a/templates/with-vercel-website/src/blocks/Form/Checkbox/index.tsx b/templates/with-vercel-website/src/blocks/Form/Checkbox/index.tsx index 0d43f93cf..633d5db0d 100644 --- a/templates/with-vercel-website/src/blocks/Form/Checkbox/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Checkbox/index.tsx @@ -39,7 +39,7 @@ export const Checkbox: React.FC< {label} - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/blocks/Form/Country/index.tsx b/templates/with-vercel-website/src/blocks/Form/Country/index.tsx index f64e77515..9c85b7539 100644 --- a/templates/with-vercel-website/src/blocks/Form/Country/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Country/index.tsx @@ -59,7 +59,7 @@ export const Country: React.FC< }} rules={{ required }} /> - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/blocks/Form/Email/index.tsx b/templates/with-vercel-website/src/blocks/Form/Email/index.tsx index 6df5112dc..fc9fd2804 100644 --- a/templates/with-vercel-website/src/blocks/Form/Email/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Email/index.tsx @@ -32,7 +32,7 @@ export const Email: React.FC< {...register(name, { pattern: /^\S[^\s@]*@\S+$/, required })} /> - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/blocks/Form/Error/index.tsx b/templates/with-vercel-website/src/blocks/Form/Error/index.tsx index 94aa85134..a7b9e47e8 100644 --- a/templates/with-vercel-website/src/blocks/Form/Error/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Error/index.tsx @@ -1,5 +1,15 @@ -import * as React from 'react' +'use client' -export const Error: React.FC = () => { - return
This field is required
+import * as React from 'react' +import { useFormContext } from 'react-hook-form' + +export const Error = ({ name }: { name: string }) => { + const { + formState: { errors }, + } = useFormContext() + return ( +
+ {(errors[name]?.message as string) || 'This field is required'} +
+ ) } diff --git a/templates/with-vercel-website/src/blocks/Form/Number/index.tsx b/templates/with-vercel-website/src/blocks/Form/Number/index.tsx index dc33d0f96..f26e54a44 100644 --- a/templates/with-vercel-website/src/blocks/Form/Number/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Number/index.tsx @@ -30,7 +30,7 @@ export const Number: React.FC< type="number" {...register(name, { required })} /> - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/blocks/Form/Select/index.tsx b/templates/with-vercel-website/src/blocks/Form/Select/index.tsx index f6de6346b..e6c159d73 100644 --- a/templates/with-vercel-website/src/blocks/Form/Select/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Select/index.tsx @@ -57,7 +57,7 @@ export const Select: React.FC< }} rules={{ required }} /> - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/blocks/Form/State/index.tsx b/templates/with-vercel-website/src/blocks/Form/State/index.tsx index f8215d1e1..29e49cae0 100644 --- a/templates/with-vercel-website/src/blocks/Form/State/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/State/index.tsx @@ -58,7 +58,7 @@ export const State: React.FC< }} rules={{ required }} /> - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/blocks/Form/Text/index.tsx b/templates/with-vercel-website/src/blocks/Form/Text/index.tsx index f70a0fbec..be1e0ff12 100644 --- a/templates/with-vercel-website/src/blocks/Form/Text/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Text/index.tsx @@ -26,7 +26,7 @@ export const Text: React.FC< )} - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/blocks/Form/Textarea/index.tsx b/templates/with-vercel-website/src/blocks/Form/Textarea/index.tsx index 9c596915e..ecb6e21af 100644 --- a/templates/with-vercel-website/src/blocks/Form/Textarea/index.tsx +++ b/templates/with-vercel-website/src/blocks/Form/Textarea/index.tsx @@ -34,7 +34,7 @@ export const Textarea: React.FC< {...register(name, { required: required })} /> - {errors[name] && } + {errors[name] && } ) } diff --git a/templates/with-vercel-website/src/collections/Posts/hooks/populateAuthors.ts b/templates/with-vercel-website/src/collections/Posts/hooks/populateAuthors.ts index 7ddb6220a..40b4fe5b4 100644 --- a/templates/with-vercel-website/src/collections/Posts/hooks/populateAuthors.ts +++ b/templates/with-vercel-website/src/collections/Posts/hooks/populateAuthors.ts @@ -6,26 +6,31 @@ import { User } from 'src/payload-types' // GraphQL will not return mutated user data that differs from the underlying schema // So we use an alternative `populatedAuthors` field to populate the user data, hidden from the admin UI export const populateAuthors: CollectionAfterReadHook = async ({ doc, req, req: { payload } }) => { - if (doc?.authors) { + if (doc?.authors && doc?.authors?.length > 0) { const authorDocs: User[] = [] for (const author of doc.authors) { - const authorDoc = await payload.findByID({ - id: typeof author === 'object' ? author?.id : author, - collection: 'users', - depth: 0, - req, - }) + try { + const authorDoc = await payload.findByID({ + id: typeof author === 'object' ? author?.id : author, + collection: 'users', + depth: 0, + }) - if (authorDoc) { - authorDocs.push(authorDoc) + if (authorDoc) { + authorDocs.push(authorDoc) + } + + if (authorDocs.length > 0) { + doc.populatedAuthors = authorDocs.map((authorDoc) => ({ + id: authorDoc.id, + name: authorDoc.name, + })) + } + } catch { + // swallow error } } - - doc.populatedAuthors = authorDocs.map((authorDoc) => ({ - id: authorDoc.id, - name: authorDoc.name, - })) } return doc