Hello!
How can I prevent the update of a value inside an array?
export const TaskLanguagesGlobal: GlobalConfig = {
fields: [
{
name: "languages",
type: "array",
required: true,
fields: [
{
name: "code",
type: "text",
required: true,
unique: true,
access: {
update: ({ data, id }) => {
console.log("😀", data);
return true;
},
},
},datais always an empty object (
{}) for me. And if I always return
falseI can't set the value to begin with.
I'm running Payload 2
Is this not possible to do?
Can the field be set to readonly?
Hey
@1049775120559898725, thanks for the reply. I tried that, but that doesn't let me set the initial value
Will you only set it once and not edit it again?
There's a couple of ways to do this depending on how you want it to work
Yeah, basically I want my content editors to be able add new rows to the array, set the code and then never change it
What if they need to edit the code?
Like, say, there's a syntax bug
the code is a iso 639 code, so they never change
ah okay
So you could have a hook
const ExampleField: Field = {
name: 'name',
type: 'text',
hooks: {
beforeValidate: [(args) => {...}],
beforeChange: [(args) => {...}],
afterChange: [(args) => {...}],
afterRead: [(args) => {...}],
}
}on the field in question
So on beforeChange, you can check the previous value, if it was empty, then allow the change
Otherwise, dont
Thanks for the help, I ended up doing it like this:
hooks: {
beforeChange: [
({ value, previousValue }) => {
if (previousValue) {
return previousValue as string;
}
return value as string;
},
],
},which makes a lot of sense, I got snowed into
accessthat I didn't think about anything else. Cheers!
Nice!!!
Working how you'd like now?
Yeah, exactly like I wanted to, thanks again!
Star
Discord
online
Get dedicated engineering support directly from the Payload team.