#siblingData is undefined in array field

4 messages · Page 1 of 1 (latest)

potent radish
#

Trying to define access rules on a field within an array. Suppose I have an array field with two fields for each item: Field A and Field B. Now, I only want Field A to be updateable if Field B has a certain value. The admin should see the field as editable if the condition is true for Field B and disabled if the expected value is not there.

My instinct would have been to use the access: { update: ({siblingData}) => siblingData.fieldB === "some value" } - however, siblingData is always undefined. Is this a bug in Payload? Or do I simply misunderstand what data is available for the access control inside of items of an array field.

granite thistle
#

Access Control functions are scoped to the operation, meaning you can have different rules for create, read, update, delete, etc. Access Control functions are executed before any changes are made and before any operations are completed. This allows you to determine if the user has the necessary permissions before fulfilling the request.

https://payloadcms.com/docs/access-control/overview

Access control only works on page load and after an document is saved. I think what your looking for might be field level conditional logic.

  fields: [
    {
      name: 'enableGreeting',
      type: 'checkbox',
      defaultValue: false,
    },
    {
      name: 'greeting',
      type: 'text',
      admin: {
        condition: (data, siblingData, { blockData, path, user }) => {
          if (data.enableGreeting) {
            return true
          } else {
            return false
          }
        },
      },
    },
  ]

https://payloadcms.com/docs/access-control/overview

potent radish
#

@granite thistle thank you so much for responding! Well, the thing that I really want to achieve is show the field in the admin UI - always. Therefore the condition does not seem suitable. readOnly would be the best solution, but it does not take a function, so I cannot make the field conditionally editable or disabled. So all I am left with is access.update which has the problem described above. sigh