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
This commit is contained in:
committed by
GitHub
parent
4a712e1d2c
commit
74996fd511
@@ -2,12 +2,10 @@
|
||||
// default beforeDuplicate hook for required and unique fields
|
||||
import { type FieldAffectingData, type FieldHook, fieldShouldBeLocalized } from './config/types.js'
|
||||
|
||||
const unique: FieldHook = ({ value }) => (typeof value === 'string' ? `${value} - Copy` : undefined)
|
||||
const isStringValue = (value) => typeof value === 'string' && value.trim() !== ''
|
||||
const unique: FieldHook = ({ value }) => (isStringValue(value) ? `${value} - Copy` : undefined)
|
||||
const localizedUnique: FieldHook = ({ req, value }) =>
|
||||
value ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined
|
||||
const uniqueRequired: FieldHook = ({ value }) => `${value} - Copy`
|
||||
const localizedUniqueRequired: FieldHook = ({ req, value }) =>
|
||||
`${value} - ${req?.t('general:copy') ?? 'Copy'}`
|
||||
isStringValue(value) ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined
|
||||
|
||||
export const setDefaultBeforeDuplicate = (
|
||||
field: FieldAffectingData,
|
||||
@@ -18,16 +16,14 @@ export const setDefaultBeforeDuplicate = (
|
||||
(!field.hooks?.beforeDuplicate ||
|
||||
(Array.isArray(field.hooks.beforeDuplicate) && field.hooks.beforeDuplicate.length === 0))
|
||||
) {
|
||||
if ((field.type === 'text' || field.type === 'textarea') && field.required && field.unique) {
|
||||
field.hooks.beforeDuplicate = [
|
||||
fieldShouldBeLocalized({ field, parentIsLocalized })
|
||||
? localizedUniqueRequired
|
||||
: uniqueRequired,
|
||||
]
|
||||
} else if (field.unique) {
|
||||
field.hooks.beforeDuplicate = [
|
||||
fieldShouldBeLocalized({ field, parentIsLocalized }) ? localizedUnique : unique,
|
||||
]
|
||||
if (field.unique) {
|
||||
if (['email', 'number', 'point', 'relationship', 'select', 'upload'].includes(field.type)) {
|
||||
field.hooks.beforeDuplicate = [() => undefined]
|
||||
} else if (['code', 'json', 'text', 'textarea'].includes(field.type)) {
|
||||
field.hooks.beforeDuplicate = fieldShouldBeLocalized({ field, parentIsLocalized })
|
||||
? [localizedUnique]
|
||||
: [unique]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user