Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michele Benolli 31 posts 149 karma points
    Nov 11, 2021 @ 08:51
    Michele Benolli
    0

    Publish a content without a mandatory property

    Is there a way to publish a content without one or more required properties? I'm using the EditorModelEventManager to edit the properties of a content before they are loaded into the content edit view.

        public class EditorModelEvents
        {
            public void EditorModelEventManager_SendingContentModel(HttpActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e)
            {
                if (e.Model.ContentTypeAlias == Constants.MyPageAlias)
                {
                    //RemoveValidation(e.Model, Constants.MyPropertyAlias);
                    RemoveProperty(e.Model, Constants.MyPropertyAlias);
                }
            }
    
            private void RemoveProperty(ContentItemDisplay content, string propertyAlias)
            {
                foreach (var variant in content.Variants)
                    foreach (var tab in variant.Tabs)
                        tab.Properties = tab.Properties.Where(x => x.Alias != propertyAlias);
            }
    
            private void RemoveValidation(ContentItemDisplay content, string propertyAlias)
            {
                foreach (var variant in content.Variants)
                {
                    var property = GetProperty(variant, propertyAlias);
                    if (property != null)
                    {
                        property.Validation.Mandatory = false;
                        property.Validation.Pattern = null;
                    }
                }
            }
        } 
    

    With the RemoveValidation method I am able to skip the validation of properties set as mandatory and publish the content without even setting a value. The downside of using this method is that the properties are still visible to the user.

    However, if I remove the property from the ContentItemDisplay model with the RemoveProperty method, the content is saved correctly but some validation is performed and the publish action fails: "{content name} could not be published because some properties did not pass validation rules".

    Is there something that I can do to publish the content bypassing the property validation?

  • Corné Hoskam 81 posts 588 karma points MVP 3x c-trib
    Nov 11, 2021 @ 09:25
    Corné Hoskam
    100

    Hi Michele,

    Without having to remove the validation all together, the easiest way (besides making the field optional instead of required) would be to fill in the required field on the OnPublishing event so that it passes the validation, without the back-office user noticing anything has happened to the field that isn't visible to him/her in the first place! Would this be an option?

    Kind regards,

    Corné Hoskam

  • Michele Benolli 31 posts 149 karma points
    Nov 11, 2021 @ 10:21
    Michele Benolli
    0

    Hi Corné, thank you for your answer.

    It would be a solution for a specific type of content and properties. For each property, I need to know in advance the data type needed and set a value manually in order to pass validation.

    I'm looking for a more general solution, that can work potentially with new content types/properties.

Please Sign in or register to post replies

Write your reply to:

Draft