Design & Technology May 14, 2015
Creating a Custom Sitecore Field Validator in Twenty Minutes
Whilst working on a number of Sitecore projects recently, I discovered that while there are a lot of articles published about the various aspects of the CMS, little has been written about creating a validator, so I thought I’d document what I did to help others in the future.
First off, you obviously need to decide exactly what you need to validate. Here at Dept, we needed a validator that would warn the user if they gave the wrong type of image to a specific control. Namely, we had a type of control that should only take SVG files, so that they could be displayed well at any resolution.
This article isn’t just about validating image files; hopefully it will give you a good idea of how to validate pretty much anything that you can validate through Sitecore.
So where do we start in creating a validator? There are a few good resources for getting a good idea of what a validator can and can’t do, such as the very useful ‘Launch Sitecore’, but in terms of steps, let’s create the Sitecore side first…
Log in and expand out the content tree (pictured) until you see the section for validators, and create a folder for your validator. I chose the name ‘Custom’, but it doesn’t really matter what you put here, just name it something that will let others know that it didn’t come as standard with Sitecore, in case they want to move it or package it for a different environment.
Then add the ‘Field Validator’ by adding an item of the correct type (Validation Rule) to the folder, and set it up as follows in the below screenshot:
Note the ‘Type’ field in particular, as this is where you will create the code component of the validator for namespace and package.
Then it’s a case of actually adding it to a field that we want it displayed on.
To do this, find the Item that you want to validate a field of (folder name hidden to protect the innocent):
Add the field, if not already present, and expand it in the content tree to view the field itself. From here, add the validator to the section you want.
In this case, I’ve chosen the validation bar so that it’s displayed on the side. Here’s a quick example:
In the corresponding project for your Sitecore site, add the class which we specified earlier in Sitecore, and make it extend
StandardValidator from the Sitecore namespace.
After this, it’s just a case of over-riding the Evaluate method that Sitecore gives you to validate your rules. Here’s my simple example:
As you can see, we take in the parameter that we passed in from Sitecore earlier, and use it to validate the item.
We grab the item by parsing the
ControlValidationValue with a quick regex to get the ID of the item, then load it from the database.
This is done by pulling the item itself from the database it’s present in, so that we can validate the field (which in this case is a MediaItem), so that we can grab the extension directly for a comparison.
After we have the item, it’s a pretty quick check to see if our item is valid and, if not, set the Text of the warning to display and return it to the user.
Once your code is deployed to the site, you have yourself a fully functioning validator!