fix: beforeValidate deleting value when access returns false (#11549)
### What? Regression caused by https://github.com/payloadcms/payload/pull/11433 If a beforeChange hook was checking for a missing or undefined `value` in order to change the value before inserting into the database, data could be lost. ### Why? In #11433 the logic for setting the fallback field value was moved above the logic that cleared the value when access control returned false. ### How? This change ensures that the fallback value is passed into the beforeValidate function _and_ still available with the fallback value on siblingData if access control returns false. Fixes https://github.com/payloadcms/payload/issues/11543
This commit is contained in:
44
test/access-control/collections/hooks/index.ts
Normal file
44
test/access-control/collections/hooks/index.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { CollectionConfig } from 'payload'
|
||||
|
||||
import { hooksSlug } from '../../shared.js'
|
||||
|
||||
export const Hooks: CollectionConfig = {
|
||||
slug: hooksSlug,
|
||||
access: {
|
||||
update: () => true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'cannotMutateRequired',
|
||||
type: 'text',
|
||||
access: {
|
||||
update: () => false,
|
||||
},
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'cannotMutateNotRequired',
|
||||
type: 'text',
|
||||
access: {
|
||||
update: () => false,
|
||||
},
|
||||
hooks: {
|
||||
beforeChange: [
|
||||
({ value }) => {
|
||||
if (!value) {
|
||||
return 'no value found'
|
||||
}
|
||||
return value
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'canMutate',
|
||||
type: 'text',
|
||||
access: {
|
||||
update: () => true,
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user