andrew4582

ValidationHelper

Aug 21st, 2011
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Reflection;
  4.  
  5. namespace Core {
  6.  
  7.     /// <summary>
  8.     /// Helper class used to validate components the use the 'System.ComponentModel.DataAnnotations' validation attributes
  9.     /// </summary>
  10.     public class ValidationHelper {
  11.         /// <summary>
  12.         /// Validates the component's "Required" and "StringLength" Attributes and returns a dictionary with the Property Name as the key and the error message as the value;
  13.         /// Note: If a propery has more than one 'error message' the error message will be delimeted by a NewLine
  14.         /// </summary>
  15.         /// <param name="component">The object to validate with "Required" and "StringLength" Attributes</param>
  16.         /// <param name="errors">A dictionary used to add Property Name as the key and the error message as the value</param>
  17.         public static void AppendValidationErrors(object component,Dictionary<string,string> errors) {
  18.  
  19.             //Test for required fields
  20.             var requiredProps = TypeHelper.GetAttributes<RequiredAttribute>(component);
  21.  
  22.             foreach(MemberInfo propname in requiredProps.Keys) {
  23.  
  24.                 var att = requiredProps[propname];
  25.                 object propvalue = TypeHelper.GetValue(component,propname.Name);
  26.                 if(!att.IsValid(propvalue)) {
  27.                     string errmsg = att.ErrorMessage;
  28.                     if(errmsg.IsNullOrEmptyTrim())
  29.                         errmsg = propname + " is required";
  30.  
  31.                     //append or add the error message for the propertyname
  32.                     if(errors.ContainsKey(propname.Name))
  33.                         errors[propname.Name] += "\r\n" + errmsg;
  34.                     else
  35.                         errors.Add(propname.Name,errmsg);
  36.                 }
  37.             }
  38.  
  39.             //Test for string lengths either max or min
  40.             var stringLengthProps = TypeHelper.GetAttributes<StringLengthAttribute>(component);
  41.             foreach(MemberInfo propname in stringLengthProps.Keys) {
  42.                 var att = stringLengthProps[propname];
  43.                 var propvalue = TypeHelper.GetValue(component,propname.Name);
  44.  
  45.                 if(!att.IsValid(propvalue)) {
  46.                     string errmsg = att.ErrorMessage;
  47.                     if(errmsg.IsNullOrEmptyTrim()) {
  48.                         if(att.MinimumLength == 0) {
  49.                             errmsg = propname + " must be not greater than {0} characters".FormatString(att.MaximumLength);
  50.                         }
  51.                         else if(att.MaximumLength > 0 && att.MinimumLength > 0) {
  52.                             errmsg = propname + " must be between {0} and {1} characters".FormatString(att.MinimumLength,att.MaximumLength);
  53.                         }
  54.                         else
  55.                             errmsg = propname + " string length is invalid";
  56.                     }
  57.  
  58.                     //append or add the error message for the propertyname
  59.                     if(errors.ContainsKey(propname.Name))
  60.                         errors[propname.Name] += "\r\n" + errmsg;
  61.                     else
  62.                         errors.Add(propname.Name,errmsg);
  63.  
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment