Hooking into the Core: Pipelines and Validation in 5.3.1

The days around Easter are always wonderfull to make up your mind, get over the busy start of the year and become prepared for a just another shiny summer :). And to start blogging again as I’ve been very busy without having the change to share my thoughts. Peter already did an excellent job on describing what is changed in the last build of Sitecore. Ofcourse, I wouldn’t be myself trying to beat Peter with a pretty in-depth article on hooking in the core making advantage on some new functionality… As I speak Peter nearly on daily base on Messenger, I’m sure he won’t mind. Actually, I expect him to ‘beat’ me again later this week!

Back to Sitecore: This saturday I’ve placed a fresh 5.3.1 installation on my laptop and after a short time of running trough apps like the Content Editor, my eyes were caught by the following settings:

Some validation settings for fields and items? Hmm, interesting! So I opened the Template manager and found out that the Base Template has new field: ‘Rules’ which is a treelist  with the datasource to the validation-settings-folder. The Template field has a new field called ‘Validation rules’:

Also: Check out the ‘Base Template’ button, which is added on(not only) mine request, so base templates are easier definable.

Back to validation: let’s take a closer look at a validator. In this case the Max Length 40:

What we see is a reference to a class in the Sitecore.Kernel and the possibility to pass a parameter to this class. By now, you proparly already opened your Reflector and are looking at the namespace Sitecore.Data.Validators:

As you can see, there’s a Manager, Message, MessageType, Result and a Rule. The manager is used to apply the rules on the items and get back the results filled with messages. The other classes in the namespace are implementation based on ValidatonRule.

So I though, wow cooool! We finally get the possibility to define our own rules… But after some test I’ve found out the rules weren’t really called by the client? It seems not to be called? Or it might be used for the Media Library, but not in the way I expect it should work! It should prevent the user from saving this item…
But still, why is it so obvious available in the templates, but can’t we find the functionality? Is this an undocumented feature which will be documented in 5.4? Do I just miss the pipeline processor? Seems so! After some investigation, I’ve decided to write one, below you can find the result. Make sure you install it on the last bits of Sitecore and use it at your own risk. Akthough it should be tested pretty well:

    1 using Sitecore.Data.Items;

    2 using Sitecore.Data.Validators;

    3 using Sitecore.Pipelines.Save;

    4 

    5 namespace Sitecore.Pipelines.Save.Validation

    6 {

    7     /// <summary>

    8     /// Executes intensive validation rules on an item.

    9     /// </summary>

   10     public class ValidationRuleProcessor

   11     {

   12         public void Process(SaveArgs args)

   13         {

   14             //Walk trough the items in the pipeline arguments

   15             foreach (SaveArgs.SaveItem saveItem in args.Items)

   16             {

   17                 //Retrieve the last saved version of the item

   18                 Item orgItem = Context.ContentDatabase.Items[saveItem.ID, saveItem.Language, saveItem.Version];

   19 

   20                 //Start editing the item and overwrite the field with the saved ones

   21                 orgItem.Editing.BeginEdit();

   22                 foreach (SaveArgs.SaveField field in saveItem.Fields)

   23                 {

   24                     orgItem.Fields[field.ID].Value = field.Value;

   25                 }

   26 

   27                 //Create a result object and validate the item

   28                 ValidationResult valResult = new ValidationResult();

   29                 ValidationManager.Validate(valResult, orgItem);

   30 

   31                 //Walk trough the messages and displey them

   32                 //When a Error occurs, abort the pipeline so the item isn’t saved

   33                 foreach (ValidationMessage message in valResult.Messages)

   34                 {

   35                     Context.ClientPage.ClientResponse.Alert(message.Message);

   36 

   37                     if (message.Type == ValidationMessageType.Error)

   38                     {

   39                         args.Abort();

   40                         return;

   41                     }

   42                 }

   43 

   44                 //Reject the changes, savind will be done in a later stage of the pipeline

   45                 orgItem.Editing.RejectChanges();

   46             }

   47         }

   48     }

   49 }

Add it to the saveUi pipeline just behind the regular validation step(Sitecore.Pipelines.Save.ValidateFields, Sitecore.Kernel). And enjoy advanced validation on both Items and Fields!
Next posts, I’ll discuss the advanced possibilities you get from this kind of validation. Later on, I’ll publish some of the events, pipeline-components, etc, I’ve written lasts month(-s).

4 thoughts on “Hooking into the Core: Pipelines and Validation in 5.3.1”

  1. After a bit of fiddling around I eventually managed to get this working! I forgot to add a reference to System.Web in my project and it took me a while to discover that it was required. I’m now happily creating my custom validators.

    Alex, bedankt!

  2. Hi Alex,

    I’ve been playing around with your class for a week now and it works almost perfectly. It actually works a bit too well! When I save a master based on a template with validation, the validation events also fire. This isn’t a desired effect as the validation should only happen for users (who use masters) and not for developers (who create masters). I will try to fix this myself but since I’m relatively new to SiteCore (1 month of experience) it’ll be hard. If you happen to have a solution brewing in your brain, please let me know.

    Thanks, Sander.

Comments are closed.