This content is archived. See latest version here

Last updated: Mar 25 2013

Table of Contents

  • The ValidationService
  • The IValidate<T> interface
  • DataAnnotationsValidator
  • Content validation
  • 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.

    DataAnnotationsValidator

    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.

    Content validation

    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 System.ComponentModel.DataAnnotations.ValidationAttribute. 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);

    For more information on how to use the Validation, refer to the section "Validation� in the EPiServer Framework SDK

    Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/