2

I am using Sitecore 9.0.2 with SXA 1.8.0.

I extended the Required Field validator with the Result=CriticalError parameter, so the authors can see an alert each time they edit a page in Experience editor with components that did not pass the field validation.

I would like to hide components with field validation issues in the preview mode and on CD, is there an easy way to do this?

Some of my components are OOTB SXA components and some of them are partially/fully custom so there needs to be a way to make the solution work for all of them.

1 Answer 1

1

You could do this by overriding the ExecuteRendering processor in the renderRendering pipeline, and running the validation in there. If the PageMode.IsPreview is true, then run the validator and if the validator succeeds, then you can run run execute the rendering.

The following should give you a good start, but is not tested:

public class ExecuteRendererWithValidation : Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecureRenderer
{
    public override void Process(RenderRenderingArgs args)
    {
        if (Sitecore.Context.PageMode.IsPreview) 
        {
            // If we are in preview mode and the item validates
            // then render the component. Otherwise, hide it from the 
            // editor
            if (ValidateItem(args.PageContext.Item)){
                base.Process(args);
            }
        }
        else 
        {
            base.Process(args);
        }
    }

    private bool ValidateItem(Item item) {

        var validator = ValidatorManager.BuildValidator(item, Sitecore.Context.Item);
        validator.Validate(new ValidatorOptions(false));

        return validator.IsValid;
    }
}

You would then need to patch this processor in, instead of the default processor:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <mvc.renderRendering>
                <processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc">
                    <patch:attribute name="type">MyNamespace.ExecuteRendererWithValidation, MyAssembly</patch:attribute>
                </processor>
            </mvc.renderRendering>
        </pipelines>
    </sitecore>
</configuration>

That should then hide the rendering in preview mode if the datasource item does not validate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.