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;
5 namespace Sitecore.Pipelines.Save.Validation
7 /// <summary>
8 /// Executes intensive validation rules on an item.
9 /// </summary>
10 public class ValidationRuleProcessor
12 public void Process(SaveArgs args)
14 //Walk trough the items in the pipeline arguments
15 foreach (SaveArgs.SaveItem saveItem in args.Items)
17 //Retrieve the last saved version of the item
18 Item orgItem = Context.ContentDatabase.Items[saveItem.ID, saveItem.Language, saveItem.Version];
20 //Start editing the item and overwrite the field with the saved ones
22 foreach (SaveArgs.SaveField field in saveItem.Fields)
24 orgItem.Fields[field.ID].Value = field.Value;
27 //Create a result object and validate the item
28 ValidationResult valResult = new ValidationResult();
29 ValidationManager.Validate(valResult, orgItem);
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)
37 if (message.Type == ValidationMessageType.Error)
44 //Reject the changes, savind will be done in a later stage of the pipeline
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).