Table of Contents
The Validation service
There is a central service for validation. 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);
What the service does is that it will locate all implementaions of EPiServer.Validation.IValidate<T> during initialization. The service will then when a validation request comes for an object instance 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.
There is a base class EPiServer.Validation.DataAnnotationsValidator<T> that can be used when implemeting an validator that validates against attributes inheriting System.ComponentModel.DataAnnotations.ValidationAttribute.
EPiServer.DataFactory.Save (implementation of EPiServer.IContentRepository.Save)
will validate the content instance before it gets 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
There is a possibility to prevent validation be using the save action
EPiServer.DataAccess.SaveAction.SkipValidation flag during save like:
DataFactory.Instance.Save(page, SaveAction.Publish | SaveAction.SkipValidation);