Unfortunately, this scenario is rarely the case which is why using validators can ensure that content authors are entering valid content, thus preventing any issues with site functionality, and layout or information architecture.Sitecore comes with a set of out-of-the-box validation rules for common scenarios such as required fields, max length restrictions, email addresses and more. However, there will be cases where the out-of-the-box validation rules don’t meet your needs. This is where creating custom validation rules will come in. Fortunately, this is a relatively straightforward process.

Creating the custom validator

We’re going to use a validator that we created to validate that a general link field includes a value in the link description field (to use as the text for our link) as an example.

First, create the custom validator, which extends StandardValidator from the Sitecore namespace.

To ensure that our custom validator works with the validator button, we need to add the serializable attribute to the class as well as define the constructors. Otherwise, the validator button would throw an exception. We also override the Name property and the Evaluate method. This is where we add our custom validation code.

As you can see, we first perform some basic null checking to ensure that the field, and ControlValidationValue are not null or empty. If either of those values are empty, we return a Valid result so that the field isn’t validated any further (we only want to validate the text property if the link field has a value). We then check the value of the “text” attribute, and if this value is null, or empty, we define custom error text, and return a “CriticalError” validator result so that the author knows that there is an error, but is still able to save (more on this below).

Creating the validation rule in Sitecore

Now that we’ve created our custom validator, we need to add the validation rule item in Sitecore. Field validators are located in /sitecore/system/Settings/Validation Rules/Field Rules/.

Notice that we point the type to our custom-created class and that we’re assigning the “Result” parameter the value of “FatalError.” All validators have a parameter titled “Result” which determines what the default result of the validator will be. Values for this parameter are:

Valid

Suggestion

Warning

Error

CriticalError (Author is warned, but is still allowed to save)

FatalError (Author is prevented from saving the item)

By passing in a value to the “Result” parameter here, we can override the default value we set in code (CriticalError). You might wonder why we’re doing this, so let me explain.

You may want to apply different validation behaviour depending on how validation is triggered. In our case, we wanted validation to fatally fail and not complete the workflow, but we still wanted the author to be able to save their work and not lose progress. To accomplish this, we created two validation rule items, both pointing to the same validation class, but each passing in a different “Result” parameter (CriticalError and FatalError).

Applying the validation rule to your field

Now that we’ve created a couple rules, we want to apply them to our field requiring validation.

To do so, select the according field on the template you want to validate, and expand the “Validation Rules” section. You’ll notice there are four places to add validation; Quick Action Bar (which runs validation in the bar left of the content tree), Validate Button (which runs validation after clicking the Validation button in the review tab), Validator Bar (which runs when you save the item, and in the bar to the right of the content editor), and Workflow (which runs on the workflow action). This allows us to apply different rules depending on how validation is triggered (in our case, allow the author to save but not publish in a case where the link text is null or empty).

Conclusion

By following these steps, not only have we created a custom field validator for Sitecore, but we’ve showed how to control the validation behaviour depending on how it is triggered, thus allowing us to apply validation without impeding an author’s progress.

Michael Armstrong

Senior Solution Developer

Calgary

Senior Solution Developer Michael Armstrong is responsible for developing elegant and user friendly solutions. He leverages his expertise in front end and back end development to craft intranets, websites, and business applications that drive toward outcomes and satisfy all stakeholders. Michael’s primary focuses are the SharePoint and Sitecore platforms and his skills include a variety of technologies and languages. He is adept at solving complex problems and enjoys applyinghis development experience in both technical and non-technical ways.

Before coming to Habanero, Michael worked as an application developer at Lucidia Ltd. He graduated as a computer programmer analyst from Sault College and upon graduation, won a SSMARt Innovation Award for Student/Graduate of the Year from the Sault Ste. Marie Innovation Centre. Outside of work, Michael enjoys hockey, snowboarding, travel, fishing, golfing, and experimenting with new technology.