Stefan Forsberg
May 1, 2010
(0 votes)

Validating page data with Data Annotations

Jarle posted a very inspiring post about validating your PageData object upon save. This is a quick post that uses the same technique but uses Data Annotations for validation.

If you’re not familiar with data annotations basically it’s a way to validate your entities by decorating properties with various validation attributes that lives in the System.ComponentModel.DataAnnotations namespace.

Let’s say we have this property in our TypedPageData class

   1: [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "/validationerror/mainbodyisempty")]
   2: [System.ComponentModel.DataAnnotations.StringLength(15, ErrorMessage = "/validationerror/mainbodyistoolong")]
   3: [PageTypeProperty(SortOrder = 100, UniqueValuePerLanguage = true, Searchable = true, Type = typeof(PropertyLongString), Tab = typeof(Tabs.Information))]
   4: public virtual string MainBody
   5: {
   6:     get;
   7:     set;
   8: }


In this case I’ve used a path as the ErrorMessage to be able to use the EpiServer functionality of the language xml files. The following simple class validates a given object by finding all properties marked with a ValidationAttribute.

   1: public class PageDataValidator
   2: {
   3:     public IEnumerable<string> GetErrors(object toValidate)
   4:     {
   5:         return TypeDescriptor
   6:             .GetProperties(toValidate)
   7:             .Cast<PropertyDescriptor>()
   8:             .SelectMany(prop =>
   9:                 prop.Attributes.OfType<ValidationAttribute>(),
  10:                 (prop, attribute) => new { prop, attribute })
  11:                 .Where(@t1 => !@t1.attribute.IsValid(@t1.prop.GetValue(toValidate)))
  12:                 .Select(@t1 => LanguageManager.Instance.Translate(@t1.attribute.ErrorMessage));
  13:     }
  14: }


In the GlobalPageValidation we simply initiate our PageDataValidator class and performs some simple logic to set the appropriate error(s).

   1: void GlobalPageValidation_Validators(object sender, PageValidateEventArgs e)
   2: {
   3:     PageDataValidator validator = new PageDataValidator();
   4:     var errors = validator.GetErrors(e.Page);
   6:     if (errors.Count() > 0)
   7:     {
   8:         e.IsValid = false;
   9:         foreach(var error in errors)
  10:         {
  11:             e.ErrorMessage += error;    
  12:         }
  14:     }
  15: }
May 01, 2010


May 20, 2014 10:00 AM

Nice, works fine thank you! =)

Please login to comment.
Latest blogs
Caching & Rendering of metadata from the DAM

For the EPiServer.Cms.WelcomeIntegration.UI 1.2.0 release, we have facilitated the ability to cache and render metadata from assets within the DAM....

Matthew Slim | Jun 2, 2023

Conversion Rate Optimization: Maximizing ROI through Data-Driven Decisions

In today's digital landscape, businesses are constantly looking for ways to improve their online presence and drive more conversions. One effective...

JessWade | Jun 1, 2023

Enhance Your Writing with Suggestions

Are you tired of staring at a blank screen, struggling to find the right words? The Epicweb.Optimizely.AIAssistant Addon is here to revolutionize...

Luc Gosso (MVP) | May 31, 2023 | Syndicated blog

Content Graph - Letting GraphQL do all the hard work for you

Background As we have seen before, setting up Content Graph on the CMS side is pretty easy. However, when it comes to the “head” part of the setup,...

Kunal Shetye | May 26, 2023 | Syndicated blog

Improved headless functionality in Customized Commerce

Did you know that with the release of Content Delivery Commerce API 3.7 we have massively improved the out of the box headless capabilities of...

Marcus Hoffmann | May 25, 2023

Boost Your Productivity with the AI Assistant Addon for Optimizely Content Cloud

In today's fast-paced digital world, efficiency and convenience are paramount. That's why we're excited to introduce the Optimizely AI-Assistant...

Luc Gosso (MVP) | May 25, 2023 | Syndicated blog