admin icon indicating copy to clipboard operation
admin copied to clipboard

Not able to make fields readonly in edit mode

Open aniruddha-mmt-zz opened this issue 7 years ago • 3 comments

Hi Jinzhu and team. Awesome product to make the life of go developers easy especially which came from python/Django background. I'm stuck while trying to make some of my model fields read-only i.e. once saved, the fields should not be allowed for editing. Please help or suggest any other way.

aniruddha-mmt-zz avatar Aug 09 '18 13:08 aniruddha-mmt-zz

Configuring the meta type as read-only does not work for you?

Than you might have to work with custom Setter to ignore the updates. https://doc.getqor.com/admin/fields.html

cryptix avatar Aug 09 '18 15:08 cryptix

Type as read-only won't help me because my use case is once a user enters a value and save, it should become un-editable. I have gone through the whole doc but not able to find any specific custom type, could you please share some sample code snippet.

aniruddha-mmt-zz avatar Aug 10 '18 09:08 aniruddha-mmt-zz

Search for virtual field in the link I posted. There you will find this code for passwords.

user.Meta(&admin.Meta{Name: "Password",
    Type:   "password",
    Valuer: func(interface{}, *qor.Context) interface{} { return "" },
    Setter: func(record interface{}, metaValue *resource.MetaValue, context *qor.Context) {
        if newPassword := utils.ToString(metaValue.Value); newPassword != "" {
            bcryptPassword, _ := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
            record.(*models.User).EncryptedPassword = string(bcryptPassword)
        }
    },
})

for your application, I'd just check if the value was previously saved and discard any changes. This won't make it disabled in the HTML, though.

cryptix avatar Aug 10 '18 10:08 cryptix