Coder Social home page Coder Social logo

Comments (5)

jmikrut avatar jmikrut commented on June 22, 2024

Hey @richardvanbergen ,

This is actually intended behavior that we settled on while designing the readOnly feature.

We thought that in order for a field to be "read", its value must be set in the first place. While a value could be set with hooks or through APIs, given that this command only affects the admin panel, we determined that readOnly should only affect update operations.

To disable your field within the create operation, you can set an access control function on it like below:

{
  label: 'S3 URL',
  name: 's3Url',
  type: 'text',
  admin: {
    readOnly: true,
  },
  access: {
    create: () => false,
  },
  hooks: {
    beforeChange: [
      (): undefined => undefined,
    ],
    afterRead: [
      ({ data }): string => {
        return getUrl(String(data.filename))
      }
    ]
  }
}

I am open to revisiting the readOnly function though. We use it on things like Transactions or Orders, where an admin may need to manually place an order for a customer upfront, and have the ability to create all fields, but then not be able to go back and re-edit.

However, our above use-case could be solved the same way through access control functions. Thoughts?

@denolfe @DanRibbens

from payload.

jmikrut avatar jmikrut commented on June 22, 2024

Update:

Changing readOnly to affect both create and update will only require one line to be removed:

https://github.com/payloadcms/payload/blob/master/src/admin/components/forms/RenderFields/index.tsx#L78

Nice and easy. I'm up for it...

from payload.

denolfe avatar denolfe commented on June 22, 2024

@richardvanbergen In addition to the above fix, you'll likely want to use a condition to hide the field if it is not populated like this:

admin: {
  readOnly: true,
  condition: (data) => data?.s3Url,
}

from payload.

DanRibbens avatar DanRibbens commented on June 22, 2024

I agree, readOnly should be applied consistently.

In a future update we could allow a function call that returns boolean to give devs more control. That complicates the admin UI a little so we can wait on that for now.

from payload.

richardvanbergen avatar richardvanbergen commented on June 22, 2024

Thanks guys that all makes a lot of sense. I didn't really have a use case for it because we're all about the content editing and less so about transactions.

Sorry about the late reply I kind of forgot I filed this bug report 😆

from payload.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.