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.
This commit is contained in:
@@ -114,6 +114,7 @@ export const promise = async ({
|
||||
const pathSegments = path ? path.split('.') : []
|
||||
const schemaPathSegments = schemaPath ? schemaPath.split('.') : []
|
||||
const indexPathSegments = indexPath ? indexPath.split('-').filter(Boolean)?.map(Number) : []
|
||||
let removedFieldValue = false
|
||||
|
||||
if (
|
||||
fieldAffectsData(field) &&
|
||||
@@ -121,6 +122,7 @@ export const promise = async ({
|
||||
typeof siblingDoc[field.name!] !== 'undefined' &&
|
||||
!showHiddenFields
|
||||
) {
|
||||
removedFieldValue = true
|
||||
delete siblingDoc[field.name!]
|
||||
}
|
||||
|
||||
@@ -331,7 +333,7 @@ export const promise = async ({
|
||||
// Execute access control
|
||||
let allowDefaultValue = true
|
||||
if (triggerAccessControl && field.access && field.access.read) {
|
||||
const result = overrideAccess
|
||||
const canReadField = overrideAccess
|
||||
? true
|
||||
: await field.access.read({
|
||||
id: doc.id as number | string,
|
||||
@@ -342,7 +344,7 @@ export const promise = async ({
|
||||
siblingData: siblingDoc,
|
||||
})
|
||||
|
||||
if (!result) {
|
||||
if (!canReadField) {
|
||||
allowDefaultValue = false
|
||||
delete siblingDoc[field.name!]
|
||||
}
|
||||
@@ -351,6 +353,7 @@ export const promise = async ({
|
||||
// Set defaultValue on the field for globals being returned without being first created
|
||||
// or collection documents created prior to having a default
|
||||
if (
|
||||
!removedFieldValue &&
|
||||
allowDefaultValue &&
|
||||
typeof siblingDoc[field.name!] === 'undefined' &&
|
||||
typeof field.defaultValue !== 'undefined'
|
||||
|
||||
@@ -484,6 +484,12 @@ export default buildConfigWithDefaults(
|
||||
type: 'checkbox',
|
||||
hidden: true,
|
||||
},
|
||||
{
|
||||
name: 'hiddenWithDefault',
|
||||
type: 'text',
|
||||
hidden: true,
|
||||
defaultValue: 'default value',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -231,6 +231,26 @@ describe('Access Control', () => {
|
||||
expect(updatedDoc.cannotMutateRequired).toBe('cannotMutateRequired')
|
||||
expect(updatedDoc.cannotMutateNotRequired).toBe('cannotMutateNotRequired')
|
||||
})
|
||||
|
||||
it('should not return default values for hidden fields with values', async () => {
|
||||
const doc = await payload.create({
|
||||
collection: hiddenFieldsSlug,
|
||||
data: {
|
||||
title: 'Test Title',
|
||||
},
|
||||
showHiddenFields: true,
|
||||
})
|
||||
|
||||
expect(doc.hiddenWithDefault).toBe('default value')
|
||||
|
||||
const findDoc2 = await payload.findByID({
|
||||
id: doc.id,
|
||||
collection: hiddenFieldsSlug,
|
||||
overrideAccess: false,
|
||||
})
|
||||
|
||||
expect(findDoc2.hiddenWithDefault).toBeUndefined()
|
||||
})
|
||||
})
|
||||
describe('Collections', () => {
|
||||
describe('restricted collection', () => {
|
||||
|
||||
Reference in New Issue
Block a user