Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Introduction

The Validation service is a central service for validation of object instances. The service implements the interface EPiServer.Validation.IValidationService and the service instance can be retrieved from the IOC container. The service have a single method defined as: IEnumerable<ValidationError> Validate(object instance);
The service will locate all implementations of EPiServer.Validation.IValidate<T> during initialization. When a validation request comes for an object instance, the service will check which of the registered validators that can be assigned from the passed in object, and those validators will then be called to perform validation.

The IValidate<T> interface

To implement a validator you should create a class that implements the interface EPiServer.Validation.IValidate<T>. The interface has a single method defined as:
IEnumerable<ValidationError> Validate(T instance);
There is no need for an explicit registration. The validation service will during initialization locate all classes that implements the interface. When the ValidationService is called to validate an object instance, each validator where the object instance can be assigned to will be called. This means for example that you can implement a validator for an interface or an base class, and then that validator will be called whenever an object that implements/inherits the registered type is to be validated.

DataAnnotationsValidator

There is a base class EPiServer.Validation.DataAnnotationsValidator<T> that can be used when implemeting a validator that validates against attributes inheriting System.ComponentModel.DataAnnotations.ValidationAttribute.

Content validation

EPiServer.DataFactory.Save (implementation of EPiServer.IContentRepository.Save) will validate the content instance before it is saved to verify for example that required properties are set. It will also validate the properties on the model class (the class inheriting PageData or BlockData or that implements IContent) against any attributes inheriting from System.ComponentModel.DataAnnotations.ValidationAttribute. There is a possibility to prevent validation by using the save action EPiServer.DataAccess.SaveAction.SkipValidation flag during save like:
DataFactory.Instance.Save(page, SaveAction.Publish | SaveAction.SkipValidation);

Do you find this information helpful? Please log in to provide feedback.

Last updated: Mar 31, 2014

Recommended reading