DCSIMG
Building ASP.NET Validator using Data Annotations - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Building ASP.NET Validator using Data Annotations

Building ASP.NET Validator using Data Annotations

In this post I’m Building ASP.NET Validator using Data Annotations
going to describe
an implementation
of ASP.NET validator
that uses the new
System.ComponentModel.DataAnnotations 
assembly that comes in .NET 4.

The Validator

In order to create the validator you need to inherit from
the BaseValidator abstract class and implement its EvaluateIsValid
method.
I’ve created an ASP.NET Server Control project and added a control
class that do the exact thing.You need to reference the 
System.ComponentModel.DataAnnotations assembly in order for
it to work. Here is how it’s implemented:

namespace Validators
{
  [ToolboxData("<{0}:DataAnnotationValidator runat=server></{0}:DataAnnotationValidator>")]
  public class DataAnnotationValidator : BaseValidator
  {
    #region Properties
 
    /// <summary>
    /// The type of the source to check
    /// </summary>
    public string SourceTypeName { get; set; }
 
    /// <summary>
    /// The property that is annotated
    /// </summary>
    public string PropertyName { get; set; }
 
    #endregion
 
    #region Methods
 
    protected override bool EvaluateIsValid()
    {
      // get the type that we are going to validate
      Type source = GetValidatedType();
 
      // get the property to validate
      PropertyInfo property = GetValidatedProperty(source);
 
      // get the control validation value
      string value = GetControlValidationValue(ControlToValidate);
 
      foreach (var attribute in property.GetCustomAttributes(typeof(ValidationAttribute), true).OfType<ValidationAttribute>())
      {
        if (!attribute.IsValid(value))
        {
          ErrorMessage = attribute.ErrorMessage;
          return false;
        }
      }
      return true;
    }
   
    private Type GetValidatedType()
    {
      if (string.IsNullOrEmpty(SourceTypeName))
      {
        throw new InvalidOperationException("Null SourceTypeName can't be validated");
      }
      
      Type validatedType = Type.GetType(SourceTypeName);
      if (validatedType == null)
      {
        throw new InvalidOperationException(
            string.Format("{0}:{1}", "Invalid SourceTypeName", SourceTypeName));
      }
 
      return validatedType;
    }
 
    private PropertyInfo GetValidatedProperty(Type source)
    {
      PropertyInfo property = source.GetProperty(PropertyName, 
        BindingFlags.Public | BindingFlags.Instance);
 
      if (property == null)
      {
        throw new InvalidOperationException(
          string.Format("{0}:{1}", "Validated Property Does Not Exists", PropertyName));
      }
      return property;
    }
 
    #endregion
  }
}

 

As you can see I use two main properties in the validator
which help me to get the relevant source type name and
property to validate.

Testing The Validator

In order to test the validator I have created a Web Application
project and put inside of it a simple Person class which
uses Data Annotations for validation:

namespace ASPNETDataAnnotation
{
  public class Person
  {
    [Required(ErrorMessage="ID is requiered")]
    public string ID { get; set; }
 
    [StringLength(20, ErrorMessage = "First name is too long!")]
    public string FirstName { get; set; }
 
    [StringLength(20, ErrorMessage="Last name is too long!")]
    public string LastName { get; set; }
 
    [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
      ErrorMessage="Must be a valid e-mail address")]
    public string Email { get; set; }
  }
}

Also I’ve created a web form that uses the validator:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ASPNETDataAnnotation.WebForm1" %>
 
<%@ Register Assembly="Validators" Namespace="Validators" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:Label ID="lblID" Text="ID: " runat="server" />
            <asp:TextBox ID="txtID" runat="server" />
            <cc1:DataAnnotationValidator ID="valID" runat="server" ControlToValidate="txtID"
                PropertyName="ID" SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
        </div>
        <div>
            <asp:Label ID="lblFirstName" Text="First Name: " runat="server" />
            <asp:TextBox ID="txtFirstName" runat="server" />
            <cc1:DataAnnotationValidator ID="valFirstName" runat="server" ControlToValidate="txtFirstName"
                PropertyName="FirstName" SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
        </div>
        <div>
            <asp:Label ID="lblLastName" Text="Last Name: " runat="server" />
            <asp:TextBox ID="txtLastName" runat="server" />
            <cc1:DataAnnotationValidator ID="valLastName" runat="server" ControlToValidate="txtLastName"
                PropertyName="LastName" SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
        </div>
        <div>
            <asp:Label ID="lblEmail" Text="E-Mail: " runat="server" />
            <asp:TextBox ID="txtEmail" runat="server" />
            <cc1:DataAnnotationValidator ID="valEmail" runat="server" ControlToValidate="txtEmail"
                PropertyName="Email" SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
        </div>
        <div>
            <asp:Button ID="btnValidate" Text="Validate" runat="server" />
        </div>
    </div>
    </form>
</body>
</html>

 

Running the web form and clicking on the button while we
have bad data will generate validation error as expected:
WebForm Validation Results

Summary

In the post I showed one way to create an ASP.NET data annotation
validator. In other frameworks like ASP.NET MVC or WCF RIA Services
you have such validators implemented.

Posted: Apr 08 2010, 07:48 AM by Gil Fink | with 6 comment(s) |
תגים:,

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 8, 2010 7:52 AM

Twitter Trackbacks for NEW: Building ASP.NET Validator using Data Annotations - Gil Fink on .Net - #dataannotations #Validators [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 NEW: Building ASP.NET Validator using Data Annotations - Gil Fink on .Net -  #dataannotations #Validators         [microsoft.co.il]        on Topsy.com

# April 9, 2010 2:05 AM

VB.Net & ASP.Net Developer / Tutor (Karachi) | Software … | VB WebDev Insider said:

Pingback from  VB.Net &amp; ASP.Net Developer / Tutor (Karachi) | Software &#8230; | VB WebDev Insider

# April 9, 2010 1:21 PM

Ricardo Peres said:

That's strange... my comment disappeared...

# April 11, 2010 3:08 AM

Raj said:

hmm, Ricardo, Please post it again. we are eagar to read that.

# April 29, 2010 8:59 AM

Using the jQuery Validation plugin to build a Data Annotations ASP.NET Validator « Peter Mourfield's Blog said:

Pingback from  Using the jQuery Validation plugin to build a Data Annotations ASP.NET Validator &laquo; Peter Mourfield&#039;s Blog

# September 28, 2011 8:22 PM