Make a page property ReadOnly dynamically based on some condition.

Vote:
 

I have a string type PageProperty. I have not make it ReadOnly. I am setting default value for this property in the SetDefaultValue method. After setting it to a default value, I need to make it ReadOnly based on a condition. I have written following code snippet in SetDefaultValue method to try making it readonly.

                this.SampleProperty= "Lorem Ipsum";

                PropertyDescriptor descriptor = TypeDescriptor.GetProperties(GetType())["SampleProperty"];

                ReadOnlyAttribute attrib = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];

                FieldInfo isReadOnly = attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);

                isReadOnly.SetValue(attrib,true);

But it is shwing error "lazy load property value is not supported by the current property instance" in the console.

Any ideas about how can we make it happen?

Thanks.

#182753
Edited, Sep 28, 2017 15:06
Vote:
 

What is the requirement? You don't want certain admins to change the value?

Maybe you can create a custom validation attribute instead?

#182763
Sep 28, 2017 16:06
Vote:
 

Hi Johan,

Thanks for replying.

Our requirement is 

  1. We need to set default value for that page property.
  2. After setting it to default value and checking a condition that current Page's parent is a particular page say "ParentPage", we need to make it ReadOnly. If current Page's parent is not "ParentPage", we need to keep it as it is.

Note: We are not making that property ReadOnly explicitly. 

Thanks.

#182767
Sep 28, 2017 16:18
Vote:
 

In that case I would either create a custom validation attribute or custom validator, please see https://world.episerver.com/documentation/developer-guides/CMS/Content/Validation/.

If you go with a custom validator, IValidate<T>, you have easy access to the content and can check its parents. This will not make the property editor read-only though, but it will stop admins from changing the value. You can also extended with a custom editor (dojo/dijit) that makes the editor read-only as well, but that seems a bit overkill.

I would not recommend putting this logic inside the content model itself.

#182768
Sep 28, 2017 16:27
Vote:
 

Hi

It should be possible to modify the metadata to make the property show up as read only as well.

Either you can implement an IMetadataExtender and put the logic there, like this pattern: https://tedgustaf.com/blog/2016/denote-required-properties-with-an-asterisk-in-episerver/

Or you can add an EditorDescriptor and decorate your property with a UiHint that you also set on the desciptor. Here is an example of that (except for the UiHint part: http://penguinoid.com/?p=337

Regards

Per Gunsarfs

#182769
Sep 28, 2017 16:42
Vote:
 

Hi

I posted about using a IMetadataExtender here: https://www.david-tec.com/2017/07/hiding-required-properties-on-the-create-new-page-in-episerver/

You can use the same approach to set the ReadOnly on the property based on the parent.

David

#182775
Sep 28, 2017 21:37
Vote:
 
<div class="blockQuote"> <div class="borderBottom"><a href="https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2017/9/make-a-page-property-readonly-dynamically-based-on-some-condition--/remake-a-page-property-readonly-dynamically-based-on-some-condition5--/">David Knipe said:</a></div> <p>Hi</p> <p>I posted about using a IMetadataExtender here:&nbsp;<a href="https://www.david-tec.com/2017/07/hiding-required-properties-on-the-create-new-page-in-episerver/">https://www.david-tec.com/2017/07/hiding-required-properties-on-the-create-new-page-in-episerver/</a></p> <p>You can use the same approach to set the ReadOnly on the property based on the parent.</p> <p>David</p> </div> <p>Thanks David.</p> <p>It worked like charm.</p>
#182779
Sep 29, 2017 7:56
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.