Make Property readonly/Hide/Ignore after Content Creation

Vote:
 

Hi,

I have created Required property which I used to set value for a different property during page creation.

Now I want to remove/hide/ignore this required property after content creation.

I am unable to change the metadata of this property in the content creation event.

Any Suggestion?

#208168
Oct 16, 2019 9:50
Vote:
 
#208170
Oct 16, 2019 10:18
Vote:
 

Hi Jayesh,

I think that, to do what you want, you'll need to create a custom validator which is sets your field as required when the value is not set then hides the field from the editor once it's set. You can do that like this:

public class InitialValidator : ValidationAttribute, IMetadataAware
{
    public void OnMetadataCreated(ModelMetadata metadata)
    {
        var contentMetadata = metadata as ContentDataMetadata;
        if (contentMetadata.InitialValue != null)
        {
            metadata.IsRequired = false;
            metadata.ShowForEdit = false;
            return;
        }
        metadata.IsRequired = true;
    }
}

Which you could then apply just by adding a [InitialValidator] attribute to the property which you only want to show on creation.

#208171
Oct 16, 2019 11:09
JayeshM - Oct 16, 2019 17:46
Thanks, Paul. This works.
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.