Stefan Forsberg
May 1, 2010
  8390
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024