What is the requirement? You don't want certain admins to change the value?
Maybe you can create a custom validation attribute instead?
Hi Johan,
Thanks for replying.
Our requirement is
Note: We are not making that property ReadOnly explicitly.
Thanks.
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.
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
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
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.