Template dependent field validator WITHOUT Sitecore rules engine

A year ago I have posted about how to create template dependent field validator with rules engine. But it requires too many modifications, so I have decided to write this post about how to do the similar functionality without rules engine.

So at first I have implemented a base class which should be used by all validators which want to be dependent on a template or a base template. This class does the following steps:

  1. Checks the template ID and base template ID of the current item
  2. If it is true then the main part is called, this is the following line:
return evaluate();

This is the injected specific validation method which is passed as a parameter.

namespace Helix.Skeleton.Foundation.Pages.Validators.Base
{
using System;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Validators;
public class BaseTemplateDependentValidator : IBaseTemplateDependentValidator
{
private const string TemplateIdToFindParameterName = "TemplateIdToFind";
public ValidatorResult Evaluate<T>(
T baseValidator,
Func<ValidatorResult> evaluate,
Func<Item> getItem)
where T : StandardValidator
{
var templateIdParameter = baseValidator.Parameters[TemplateIdToFindParameterName];
if (string.IsNullOrWhiteSpace(templateIdParameter))
{
return evaluate();
}
var templateIdToFind = ID.Parse(templateIdParameter);
var contextItem = getItem();
if (contextItem.TemplateID == templateIdToFind
|| this.CheckBaseTemplatesRecursively(contextItem.Template, templateIdToFind))
{
return evaluate();
}
return ValidatorResult.Valid;
}
private bool CheckBaseTemplatesRecursively(
TemplateItem templateItem,
ID templateIdToFind)
{
if (templateItem == null || templateItem.BaseTemplates == null)
{
return false;
}
foreach (var baseTemplate in templateItem.BaseTemplates)
{
if (baseTemplate.ID == templateIdToFind
|| this.CheckBaseTemplatesRecursively(baseTemplate, templateIdToFind))
{
return true;
}
}
return false;
}
}
}
namespace Helix.Skeleton.Foundation.Pages.Validators.Base
{
using System;
using Sitecore.Data.Items;
using Sitecore.Data.Validators;
public interface IBaseTemplateDependentValidator
{
ValidatorResult Evaluate<T>(T baseValidator, Func<ValidatorResult> evaluate, Func<Item> getItem)
where T : StandardValidator;
}
}

So we have already the base class. This is doing the most of the job, what is left is to inject our specific validator. Here you can see an example with the RequiredFieldValidator.

namespace Helix.Skeleton.Foundation.Pages.Validators
{
using System;
using System.Runtime.Serialization;
using System.Web.Mvc;
using Base;
using Sitecore.Data.Validators;
using Sitecore.Data.Validators.FieldValidators;
[Serializable]
public class TemplateDependentRequiredValidator : RequiredFieldValidator
{
public TemplateDependentRequiredValidator() : base()
{
}
public TemplateDependentRequiredValidator(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
protected IBaseTemplateDependentValidator BaseTemplateDependentValidator
{
get
{
return DependencyResolver.Current.GetService<IBaseTemplateDependentValidator>();
}
}
protected override ValidatorResult Evaluate()
{
return this.BaseTemplateDependentValidator.Evaluate<RequiredFieldValidator>(
this,
() => base.Evaluate(),
() => this.GetItem());
}
}
}

The last thing we have to do is to create the proper Validation Rule item in Sitecore. The TemplateIdToFind is the ID of the template or the base template which defines, on which items should the validator run. To do this, navigate to

/sitecore/system/Settings/Validation Rules/Field Rules

and create your Validation Rule item from

/sitecore/templates/System/Validation/Validation Rule

Capture

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s