EntLib Validation Application Block - Subclassing PropertyProxyValidator for ValueConvert

The Enterprise Library VAB provides the PropertyProxyValidator for ASP.NET applications so that you can attach it to a single control and it will display any validation messages for that business object property.  This automatic UI validation is great because it avoids duplicating your business layer validation logic in your UI.  Additionally, you can just use ONE validator control whereas using traditional ASP.NET validation controls you might need to attach 3-4 validation controls to a single UI element (e.g., RequireFieldValidator, RangeValidator, RegexValidator, etc.) which can clutter your aspx pages quickly.

However, one of the gotchas with working with the PropertyProxyValidator control is that, while it is quite elegant for strings, you have to do a little extra work for data types such as DateTime.  Specifically, if you are working with a DateTime, you have to attach a ValueConvert method to your PropertyProxyValidator control in which you perform the data type conversion.  The following code snippet is directly out of the VAB documentation:

   1:  protected void dateOfBirthValidator_ValueConvert(object sender, ValueConvertEventArgs e)
   2:  {
   3:          string value = e.ValueToConvert as string;
   4:          try
   5:          {
   6:              e.ConvertedValue = DateTime.Parse(value, System.Globalization.CultureInfo.CurrentCulture);
   7:          }
   8:          catch
   9:          {
  10:              e.ConversionErrorMessage = "Date Of Birth is not in the correct format.";
  11:              e.ConvertedValue = null;
  12:          }
  13:  }

This extensibility is great but if I need to perform multiple DateTime validations like this throughout my application, I don't want to have to copy/paste this code everywhere.  Even if I encapsulate it in a static utility method, I still have to go through the pain of wiring up an event for any control where I need this just to make that 1-line call to the utility method.

So...a better approach is to simply subclass the PropertyProxyValidator control.  In the following example, I just wire up the conversion in the contructor.  This enables me to avoid having to wire up the event in the aspx code and and also enables me to avoid having to include the event in the code behind.  I just specify the DateTimePropertyProxyValidator in my aspx code the normal way:

   1:  <asp:TextBox id="txtDateOfBirth" runat="server" />
   2:  <vab:DateTimePropertyProxyValidator ID="dobVal" runat="server" ControlToValidate="txtDateOfBirth" PropertyName="DateOfBirth" SourceTypeName="Person"/>

The same pattern could obviously be used for other data types.  Below is the complete code for the DateTimePropertyProxyValidator:

   1:  public class DateTimePropertyProxyValidator : PropertyProxyValidator
   2:  {
   3:      public DateTimePropertyProxyValidator()
   4:      {
   5:          this.ValueConvert += OnValueConvert;
   6:      }
   8:      private void OnValueConvert(object sender, ValueConvertEventArgs e)
   9:      {
  10:          DateTime result;
  12:          if (DateTime.TryParse((string)e.ValueToConvert, out result))
  13:          {
  14:              e.ConvertedValue = result;
  15:          }
  16:          else
  17:          {
  18:              e.ConversionErrorMessage = "Date is not in the correct format.";
  19:              e.ConvertedValue = null;
  20:          }
  21:      }
  22:  }
posted on Monday, June 16, 2008 3:19 PM Print
# re: EntLib Validation Application Block - Subclassing PropertyProxyValidator for ValueConvert
Richard Collette
8/9/2010 5:02 PM
Alternately, you can create a date control that has a property that is of type Nullable<DateTime>, add the ValidationProperty attribute to the class specifying the name of that property. Now you will get back either Nothing or a DateTime that works with the validation logic.

The only caveat to this is that the business object you validate with also has to have a property of Nullable<DateTime> otherwise you will get cast exception for empty values.

Post Comment

Title *
Name *
Comment *  

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers

Google My Blog

Tag Cloud