World is now on Opti ID! Learn more

Martin Pickering
Mar 11, 2014
  4931
(0 votes)

Integrating the EPiServer LocalizationService with MVC Validation Attributes

The code-first content modelling technique made main stream by EPiServer CMS 7 and now almost universal with EPiServer 7.5 and EPiServer Commerce 7 makes extensive use of the DataAnnotations available within ASP.Net. This has enriched and strengthened the Edit Mode experience for our beloved Content Editors and allows Implementers a relatively easy way to customize the Error and Warning Messages issued by EPiServer’s Edit Mode views when an attempt to violate the Content Model is detected.

Simply put, by setting the ErrorMessage property of a Validation Annotation class attached to a Content Property to a string starting with “/”, EPiServer CMS Edit Mode will attempt to use the value as a Localization Service Resource Key during On-Page Edit and All Properties View interactions.

For example,

public class SimplePage : PageData
{
   [Display(Name = "Heading",
      Description = "The Main Heading (h1) for the Page.",
      GroupName = SystemTabNames.Content,
      Order = 10)]
   [Required(ErrorMessage="/contenttypes/SimplePage/headingrequired"]
   public virtual string Heading { get; set; }
}

The thing is though, there is no such integration for the View Models one may create to support a custom application with a need to interact with the Visitor, such as a Cart or Checkout application or indeed a custom logon page. Therefore, here is but one idea as to how this might be achieved. I am sure there are a legion of other ways and I know of at least one other by Vladimir Levchuk.

My solution for integrating the EPiServer Localization Service with MVC View Models and Data Annotations is to:

  • Override the MVC CachedDataAnnotationsModelMetadataProvider to be able to extend the Model Metadata a little bit.
    Basically, detect an instance of an annotation class that has its ErrorMessage property set with a string value starting with "/"; storing in a custom Model Metadata property the value of the resource key.
  • Create or override (as appropriate) Attribute Adapter classes for the desired Annotation Attribute types used by the application's View Models: such as RequiredAttributeAdapter and StringLengthAttributeAdapter. The Attribute Adapter classes look for the Model Metadata custom property created by the Model Metadata Provider and then use the Localization Service to obtain the translated, custom error message value to use during Model Validation, whether that be Server or Client side validation.

So taking, as an example, a fairly typical Logon View Model:

public class LogonViewModel
{
   [Required(AllowEmptyStrings = false, ErrorMessage = "/contenttypes/logonpage/username/required")]
   [EmailAddress(ErrorMessage = "/contenttypes/logonpage/username/mustbeanemail")]
   public string Username { get; set; }
 
   [Required(AllowEmptyStrings = false, ErrorMessage = "/contenttypes/logonpage/password/required")]
   [StringLength(15, MinimumLength = 6, ErrorMessage = "/contenttypes/logonpage/password/lengthproblem")]
   [DataType(DataType.Password)]
   public string Password { get; set; }
 
   [DataType(DataType.Password)]
   [System.Web.Mvc.Compare("Password", ErrorMessage = "/contenttypes/logonpage/confirmpwd/doesnotcompare")]
   public string ConfirmPassword { get; set; }
}

We see that we will need at the very least Attribute Adapters for Required, String Length, Email and Compare Attributes. The great thing about these Attribute Adapters, once written they can be re-used over and over again.

So here are our Attribute Adapters...

public class LocalisableRequiredAnnotationsAdapter: RequiredAttributeAdapter 
{
    public LocalisableRequiredAnnotationsAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute) : base(metadata, context, attribute)
    {
        LocalisableAnnotationAdapterInitialiser.Initialise(metadata, attribute);
    }
 
    public static void SelfRegister()
    {
        DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute),
            typeof(LocalisableRequiredAnnotationsAdapter));
    }
}
 
public class LocalisableStringLengthAnnotationsAdapter : StringLengthAttributeAdapter
{
    public LocalisableStringLengthAnnotationsAdapter(ModelMetadata metadata, ControllerContext context, StringLengthAttribute attribute) : base(metadata, context, attribute)
    {
        LocalisableAnnotationAdapterInitialiser.Initialise(metadata, attribute);
    }
 
    public static void SelfRegister()
    {
        DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(StringLengthAttribute),
            typeof(LocalisableStringLengthAnnotationsAdapter));
    }
}
 
public class LocalisableEmailAddressAnnotationsAdapter : DataAnnotationsModelValidator<EmailAddressAttribute>
{
    public LocalisableEmailAddressAnnotationsAdapter(ModelMetadata metadata, ControllerContext context, EmailAddressAttribute attribute)
        : base(metadata, context, attribute)
    {
        LocalisableAnnotationAdapterInitialiser.Initialise(metadata, attribute);
    }
 
    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        return new[]
        {
            new ModelClientValidationRegexRule(ErrorMessage,
                "^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?$")
            {
                ValidationType = "email"
            }
        };
    }
 
    public static void SelfRegister()
    {
        DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAddressAttribute),
            typeof(LocalisableEmailAddressAnnotationsAdapter));
    }
}
 
public class LocalisableCompareAnnotationsAdapter : CompareAttributeAdapter
{
    public LocalisableCompareAnnotationsAdapter(ModelMetadata metadata, ControllerContext context, System.Web.Mvc.CompareAttribute attribute) : base(metadata, context, attribute)
    {
        LocalisableAnnotationAdapterInitialiser.Initialise(metadata, attribute);
    }
 
    public static void SelfRegister()
    {
        DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(System.Web.Mvc.CompareAttribute),
            typeof(LocalisableCompareAnnotationsAdapter));
    }
}
 
public class CompareAttributeAdapter : DataAnnotationsModelValidator<System.Web.Mvc.CompareAttribute>
{
    public CompareAttributeAdapter(ModelMetadata metadata, ControllerContext context, System.Web.Mvc.CompareAttribute attribute)
        : base(metadata, context, attribute)
    {    }
 
    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        return new[] { new ModelClientValidationEqualToRule(ErrorMessage, System.Web.Mvc.CompareAttribute.FormatPropertyForClientValidation(Attribute.OtherProperty)) };
    }
}
 
internal class LocalisableAnnotationAdapterInitialiser
{
    private static readonly LocalizationService LocalizationService = ServiceLocator.Current.GetInstance<LocalizationService>();
 
    public static void Initialise(ModelMetadata metadata, ValidationAttribute attribute)
    {
        object resourceKeyObj;
        string resourceKey;
        if (metadata.AdditionalValues.TryGetValue(attribute.GetHashCode().ToString(CultureInfo.InvariantCulture),
            out resourceKeyObj)
            && !string.IsNullOrWhiteSpace(resourceKey = (string)resourceKeyObj)
            && resourceKey.StartsWith("/"))
        {
            attribute.ErrorMessage = LocalizationService.GetString(resourceKey, resourceKey);
        }
    }
}

And here is our own extended implementation for CachedDataAnnotationsModelMetadataProvider...

[ServiceConfiguration(typeof(ModelMetadataProvider), Lifecycle = ServiceInstanceScope.Singleton)]
public class LocalizableModelMetadataProvider : CachedDataAnnotationsModelMetadataProvider
{
   protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
   {
      var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);
      foreach (var additionalValue in prototype.AdditionalValues)
      {
         result.AdditionalValues.Add(additionalValue.Key, additionalValue.Value);
      }
      return result;
   }
 
   protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable<Attribute> attributes, Type containerType, Type modelType, string propertyName)
   {
   var theAttributes = attributes as Attribute[] ?? attributes.ToArray();
   var prototype = base.CreateMetadataPrototype(theAttributes, containerType, modelType, propertyName);
   foreach (var a in theAttributes.OfType<ValidationAttribute>()
         .Where(a => !string.IsNullOrWhiteSpace(a.ErrorMessage) 
         && a.ErrorMessage.StartsWith("/")))
      {
         prototype.AdditionalValues.Add(a.GetHashCode().ToString(CultureInfo.InvariantCulture), a.ErrorMessage);
      }
      return prototype;
   }
}

Finally, a small EPiServer Initialization Module to register the Attribute Adapters with the ASP.Net run-time.

[InitializableModule]
[ModuleDependency(typeof (InitializationModule))]
public class DataAnnotationsModelValidatorInitialization : IInitializableModule
{
    private static bool _initialized;
 
    public void Initialize(InitializationEngine context)
    {
        if (_initialized)
        {
            return;
        }
        LocalisableRequiredAnnotationsAdapter.SelfRegister();
        LocalisableStringLengthAnnotationsAdapter.SelfRegister();
        LocalisableEmailAddressAnnotationsAdapter.SelfRegister();
        LocalisableCompareAnnotationsAdapter.SelfRegister();
        _initialized = true;
    }
 
    public void Uninitialize(InitializationEngine context)
    {  }
 
    public void Preload(string[] parameters)
    {  }
}

We're done, except of course to create the Localization Resources, either in the form of an XML File read by the standard XML Localization Provider, or in the form required by whatever other Localization Provider you are using; maybe one similar to that suggested by Jeroen Stemerdink on his Blog.

Mar 11, 2014

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |