Create a custom validator

This use case creates a new field validator to show you the mechanics of hooking the validator up. As you'll see, the routine itself on the .NET side is very simple in this example, but you will be able to incorporate any logic you need in your .NET class, allowing Sitecore to do the work of enforcing the validation routine at the appropriate time (you can decide if this is during workflow, before publication, at save time for the item, etc.).

Sitecore includes a rich list of "validators" by default. These validators fit into 3 categories:

  1. Field Level. These validators are applied at the individual field level. An example might be a required field validator. Look for these in the content tree at: /sitecore/system/Settings/Validation Rules/Field Rules
  2. Item Level. These validators will apply to the entire item (as presented in its presentation details). Look for these in the content tree at: /sitecore/system/Settings/Validation Rules/Item Rules
  3. Field Type Level. These validators will apply to every field of a certain Sitecore field type (i.e., Image fields). Look for these in the content tree at: /sitecore/system/Settings/Validation Rules/Field Types

For this article and example, we'll talk about the steps involved in creating a Field Level validator. As with many things in Sitecore, this exercise will start with an Item. Take some time to familiarize yourself with the items in the areas noted above--you'll see the types of validators described above and specific validation routines in each category. Take a look at the example Required field validator at: /sitecore/system/Settings/Validation Rules/Field Rules/Required. If you look at the fields that make up this item, you'll notice that the validator gives you the opportunity to call out the name of the assembly and class that is called for this validator.

The screenshot below shows the Required validator and the field that calls out the class (Sitecore.Data.Validators.FieldValidators.RequiredFieldValidator) and Assembly (Sitecore.Kernel).

Using Reflector, we can see the method that does the work for this validator. The class itself inherits from the StandardValidator class and includes an Evaluate method:

  1. protected override ValidatorResult Evaluate()
  2. {
  3. if (!string.IsNullOrEmpty(base.ControlValidationValue))
  4. {
  5. return ValidatorResult.Valid;
  6. }
  7. base.Text = base.GetText("Field \"{0}\" must contain a value.", new string[] { base.GetFieldDisplayName() });
  8. return base.GetFailedResult(ValidatorResult.CriticalError);
  9. }

We now have the mechanics we need to create our own routine:

  1. Create an item to represent the validator (as the Required validator does)
  2. Inherit from the StandardValidator class and override the Evaluate method

You'll notice that the ValidatorResult enum provides the ability to raise different levels of validation errors, and these will map to user interface behavior for the content author (prevent them from saving an item, simply showing them a yellow warning in the UI, etc.).

An easy way get started is to copy an existing validator item and change the Class, Assembly signature to match a class in your project. However, an even more powerful way to create this class is to use Sitecore Rocks. From the Add New Item menu, choose Sitecore-->UI-->Validator. This will create the entire class, set up the stub to override the Evaluate method. Note that you will still need to create the Sitecore item that calls this class.

Lastly, you have the ability to assign your new validator to a content field. Since this is a field level validator, it makes sense that you would decide that all fields of a certain data template would adhere to this new validation routine. As an example, you could decide that, for the Article data template in this project, for the Use case solution statement field, you're going to apply your new validator. If you expand the content tree to find that item, then expand the Article section to show the fields available in that section, you can click on the Use case solution statement field. Scroll down on the right until you see the validation options (ignore the validation text field as this is a legacy field). Your options will appear as the fields shown below:

validationlist

These choices represent the various places in the UI where this validation will become apparent to a content author. In general, you should choose all of these options.

  • Dec 18, 2013