Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Stefan Forsberg
May 1, 2010
  8347
(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);
   5:  
   6:     if (errors.Count() > 0)
   7:     {
   8:         e.IsValid = false;
   9:         foreach(var error in errors)
  10:         {
  11:             e.ErrorMessage += error;    
  12:         }
  13:         
  14:     }
  15: }
May 01, 2010

Comments

May 20, 2014 10:00 AM

Nice, works fine thank you! =)

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog