Skip to content

Commit 855a320

Browse files
fix: ensure default values are not shown when value is hidden (#13074)
Fixes #12834 `loginAttempts` was being shown in the admin panel when it should be hidden. The field is set to `hidden: true` therefore the value is removed from siblingData and passes the `allowDefaultValue` check - showing inconsistent data. This PR ensures the default value is not returned if the field has a value but was removed due to the field being hidden.
1 parent aa97f3c commit 855a320

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

packages/payload/src/fields/hooks/afterRead/promise.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ export const promise = async ({
114114
const pathSegments = path ? path.split('.') : []
115115
const schemaPathSegments = schemaPath ? schemaPath.split('.') : []
116116
const indexPathSegments = indexPath ? indexPath.split('-').filter(Boolean)?.map(Number) : []
117+
let removedFieldValue = false
117118

118119
if (
119120
fieldAffectsData(field) &&
120121
field.hidden &&
121122
typeof siblingDoc[field.name!] !== 'undefined' &&
122123
!showHiddenFields
123124
) {
125+
removedFieldValue = true
124126
delete siblingDoc[field.name!]
125127
}
126128

@@ -331,7 +333,7 @@ export const promise = async ({
331333
// Execute access control
332334
let allowDefaultValue = true
333335
if (triggerAccessControl && field.access && field.access.read) {
334-
const result = overrideAccess
336+
const canReadField = overrideAccess
335337
? true
336338
: await field.access.read({
337339
id: doc.id as number | string,
@@ -342,7 +344,7 @@ export const promise = async ({
342344
siblingData: siblingDoc,
343345
})
344346

345-
if (!result) {
347+
if (!canReadField) {
346348
allowDefaultValue = false
347349
delete siblingDoc[field.name!]
348350
}
@@ -351,6 +353,7 @@ export const promise = async ({
351353
// Set defaultValue on the field for globals being returned without being first created
352354
// or collection documents created prior to having a default
353355
if (
356+
!removedFieldValue &&
354357
allowDefaultValue &&
355358
typeof siblingDoc[field.name!] === 'undefined' &&
356359
typeof field.defaultValue !== 'undefined'

test/access-control/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,12 @@ export default buildConfigWithDefaults(
484484
type: 'checkbox',
485485
hidden: true,
486486
},
487+
{
488+
name: 'hiddenWithDefault',
489+
type: 'text',
490+
hidden: true,
491+
defaultValue: 'default value',
492+
},
487493
],
488494
},
489495
{

test/access-control/int.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,26 @@ describe('Access Control', () => {
231231
expect(updatedDoc.cannotMutateRequired).toBe('cannotMutateRequired')
232232
expect(updatedDoc.cannotMutateNotRequired).toBe('cannotMutateNotRequired')
233233
})
234+
235+
it('should not return default values for hidden fields with values', async () => {
236+
const doc = await payload.create({
237+
collection: hiddenFieldsSlug,
238+
data: {
239+
title: 'Test Title',
240+
},
241+
showHiddenFields: true,
242+
})
243+
244+
expect(doc.hiddenWithDefault).toBe('default value')
245+
246+
const findDoc2 = await payload.findByID({
247+
id: doc.id,
248+
collection: hiddenFieldsSlug,
249+
overrideAccess: false,
250+
})
251+
252+
expect(findDoc2.hiddenWithDefault).toBeUndefined()
253+
})
234254
})
235255
describe('Collections', () => {
236256
describe('restricted collection', () => {

0 commit comments

Comments
 (0)