#How to get field value for access control purposes?

11 messages · Page 1 of 1 (latest)

shut hollow
#

I want to make certain values of a field non-editable. How can I achieve that? I was thinking of having some defaultValues and I would map these values to be non-editable, but I can't find a way to access them.

shut hollow
#

How get the field data for access control?

shut hollow
#

How to get field value for access control purposes?

coral bronze
#

did you try using update property in access?

access: {
update: ({data, doc, siblingData, req}) => {
// rest of code
}
}

shut hollow
#

Yes, data returns as an empty object, and siblingData is undefined.

#

doc returns all the data, so I can't filter to return true/false for a specific value.

#

And with req, I don't think there is any parameter that correlates to the specific field that is being updated, right?

coral bronze
#

I am not sure i understand your use case.

Do you mind posting example?

I have recently implemented various use case when field is hidden, disabled, read only, etc, using both admin and access properties.

shut hollow
#

Sure,

const predefinedSections = [
{
section: 'Hero',
slug: 'hero',
},
{
section: 'Featured Products',
slug: 'featured-products',
},
];

const HomePage: GlobalConfig = {
slug: 'homepage',
access: {
read: () => true,
},

fields: [
    {
        name: 'sections',
        label: 'Sections',
        type: 'array',
        fields: [
            {
                name: 'section',
                label: 'Section Title',
                type: 'text',
            },
            {
                name: 'slug',
                label: 'Section Slug',
                type: 'text',
                required: true,

// I want to make this field read-only for the predefined sections. For example, if the user creates another section different from the predefined slugs, he'll be able to edit this field. However, the predefined ones are read-only and cannot be deleted (the user can only update the Section Title).
},
],
defaultValue: predefinedSections,
},
],

coral bronze
#

I would probably create custom Field component for that and compare value of sections field (useField) with predefinedSections