Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Prevent update of array->string in global

default discord avatar
benjick.last year
10

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;
            },
          },
        },


data

is always an empty object (

{}

) for me. And if I always return

false

I can't set the value to begin with.



I'm running Payload 2



Is this not possible to do?

  • default discord avatar
    notchrlast year
    @131748314256244736

    Can the field be set to readonly?

  • default discord avatar
    benjick.last year

    Hey

    @1049775120559898725

    , thanks for the reply. I tried that, but that doesn't let me set the initial value

  • default discord avatar
    notchrlast year

    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

  • default discord avatar
    benjick.last year

    Yeah, basically I want my content editors to be able add new rows to the array, set the code and then never change it

  • default discord avatar
    notchrlast year

    What if they need to edit the code?



    Like, say, there's a syntax bug

  • default discord avatar
    benjick.last year

    the code is a iso 639 code, so they never change

  • default discord avatar
    notchrlast year

    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

  • default discord avatar
    benjick.last year

    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

    access

    that I didn't think about anything else. Cheers!

  • default discord avatar
    notchrlast year

    Nice!!!



    Working how you'd like now?

  • default discord avatar
    benjick.last year

    Yeah, exactly like I wanted to, thanks again!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.