{"record":{"id":"3d4588fb6f8e0f25","repo":"payloadcms/payload","slug":"the-following-field-is-invalid-name","errorCode":null,"errorMessage":"The following field is invalid: ${name}","messagePattern":"The following field is invalid: (.+?)","errorType":"validation","errorClass":"ValidationError","httpStatus":400,"severity":"warning","filePath":"packages/payload/src/fields/baseFields/slug/generateSlug.ts","lineNumber":84,"sourceCode":"        // Unchanged from what's stored — already unique, so skip the collision query. Autosave\n        // resends the current slug on every tick; without this each tick runs a needless read.\n        if (slugified === storedSlug) {\n          return storedSlug\n        }\n\n        if (\n          collection &&\n          (await fieldValueExists({\n            id: originalDoc?.id,\n            collection: collection.slug,\n            draftsEnabled: hasDraftsEnabled(collection),\n            field: name,\n            locale,\n            req,\n            value: slugified,\n          }))\n        ) {\n          throw new ValidationError(\n            { errors: [{ message: req.t('error:valueMustBeUnique'), path: name }] },\n            req.t,\n          )\n        }\n\n        return slugified\n      }\n    }\n\n    // On update, preserve a slug that is already set — only fill it while empty.\n    if (operation !== 'create' && storedSlugHasValue) {\n      return storedSlug\n    }\n\n    // Derive an empty slug from its source, when present.\n    // Dedupe so two documents don't both claim it if they have the same source value.\n    // Globals have no collection to dedupe against.\n    const source = useAsSlug ? data?.[useAsSlug] : undefined","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/payload/src/fields/baseFields/slug/generateSlug.ts#L66-L102","documentation":"Thrown by the `generateSlug` beforeChange hook as a `ValidationError` when an explicitly provided slug value collides with an existing document's slug. Unlike generated slugs (which auto-dedupe with a suffix), an explicit user-supplied slug is checked for uniqueness and rejected if taken, surfacing a localized 'value must be unique' message on the field. This keeps user-chosen slugs stable rather than silently mutating them.","triggerScenarios":"Creating/updating a doc with `data.slug = 'taken'` when another doc already has that slug; two docs sharing a source field that produces the same explicit slug; localized slug colliding within the same locale.","commonSituations":"Admin UI where a user types a slug already in use; import scripts assigning duplicate slugs; renaming a doc's title whose derived slug clashes with an existing one when the slug is explicitly set.","solutions":["Generate the slug server-side and let Payload auto-dedupe (omit the explicit `slug` value).","Before saving, check uniqueness: `payload.find({ collection, where: { slug: { equals } }, limit: 1 })`.","Catch the `ValidationError` and prompt the user to pick another slug."],"exampleFix":"// before\nawait payload.create({ collection: 'posts', data: { slug: 'hello', title: 'Hi' } }) // may collide\n// after\nconst slug = await getUniqueSlug('hello') // or omit slug to let Payload generate\nawait payload.create({ collection: 'posts', data: { slug, title: 'Hi' } })","handlingStrategy":"try-catch","validationCode":"// Pre-check slug uniqueness before create/update\nconst { totalDocs } = await payload.count({\n  collection,\n  where: { and: [{ slug: { equals: proposedSlug } }, { id: { not_equals: currentId ?? 0 } }] },\n  req,\n})\nif (totalDocs > 0) throw new Error(`Slug '${proposedSlug}' is already in use`)","typeGuard":null,"tryCatchPattern":"try {\n  await payload.create({ collection, data })\n} catch (err) {\n  if (err instanceof ValidationError && err.data?.errors?.some((e) => /unique/i.test(e.message))) {\n    // prompt user for a different slug, or auto-generate one\n  }\n  throw err\n}","preventionTips":["Omit the slug to let Payload generate and auto-dedupe it.","Validate uniqueness before save for user-typed slugs.","Catch ValidationError at the form boundary and show a field error."],"tags":["fields","slug","validation","unique","hooks"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}