Skip to content

Commit 74996fd

Browse files
authored
fix: field appending on duplicate should ignore non string values (#11621)
### What? When duplicating a document with `unique` fields, we append `- Copy` to the field value. The issue is that this is happening when the field is empty resulting in values being added that look like: `undefined - Copy` or `null - Copy`. ### Why? We are not checking the incoming value in all cases. ### How? Checks the value exists, is a string, and is not just an empty space before appending `- Copy`. At first glance it looks incorrect to return required fields with `undefined` - however when duplicating a document, the new document is always created as a `draft` so it is not an issue to return `undefined`. Closes #11373
1 parent 4a712e1 commit 74996fd

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

packages/payload/src/fields/setDefaultBeforeDuplicate.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// default beforeDuplicate hook for required and unique fields
33
import { type FieldAffectingData, type FieldHook, fieldShouldBeLocalized } from './config/types.js'
44

5-
const unique: FieldHook = ({ value }) => (typeof value === 'string' ? `${value} - Copy` : undefined)
5+
const isStringValue = (value) => typeof value === 'string' && value.trim() !== ''
6+
const unique: FieldHook = ({ value }) => (isStringValue(value) ? `${value} - Copy` : undefined)
67
const localizedUnique: FieldHook = ({ req, value }) =>
7-
value ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined
8-
const uniqueRequired: FieldHook = ({ value }) => `${value} - Copy`
9-
const localizedUniqueRequired: FieldHook = ({ req, value }) =>
10-
`${value} - ${req?.t('general:copy') ?? 'Copy'}`
8+
isStringValue(value) ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined
119

1210
export const setDefaultBeforeDuplicate = (
1311
field: FieldAffectingData,
@@ -18,16 +16,14 @@ export const setDefaultBeforeDuplicate = (
1816
(!field.hooks?.beforeDuplicate ||
1917
(Array.isArray(field.hooks.beforeDuplicate) && field.hooks.beforeDuplicate.length === 0))
2018
) {
21-
if ((field.type === 'text' || field.type === 'textarea') && field.required && field.unique) {
22-
field.hooks.beforeDuplicate = [
23-
fieldShouldBeLocalized({ field, parentIsLocalized })
24-
? localizedUniqueRequired
25-
: uniqueRequired,
26-
]
27-
} else if (field.unique) {
28-
field.hooks.beforeDuplicate = [
29-
fieldShouldBeLocalized({ field, parentIsLocalized }) ? localizedUnique : unique,
30-
]
19+
if (field.unique) {
20+
if (['email', 'number', 'point', 'relationship', 'select', 'upload'].includes(field.type)) {
21+
field.hooks.beforeDuplicate = [() => undefined]
22+
} else if (['code', 'json', 'text', 'textarea'].includes(field.type)) {
23+
field.hooks.beforeDuplicate = fieldShouldBeLocalized({ field, parentIsLocalized })
24+
? [localizedUnique]
25+
: [unique]
26+
}
3127
}
3228
}
3329
}

0 commit comments

Comments
 (0)