Extending ValidationBase to Validate Properties of Type ValidationBase

A couple of days ago I got an e-mail from Maarten van der Lee with some code that can be used in my Validation Framework of my N-Layer Design article series to validate sub properties whose type is a ValidationBase as well. This can be useful if you want to present a full list of validation errors for an object and its properties.

Extending ValidationBase

Consider the ContactPerson class from my article series on N-Layer design. This class has an Addresses collection that contains zero or more addresses. But what if the ContactPerson only had a single address whose type is Address (which inherits ValidationBase)? In that case, when validating the ContactPerson, you may also want to validate the Address in one fell swoop and show a list of errors to your users like this:

  • Enter your first name
  • Enter your zip code
  • Choose an address type

Fortunately, with just a bit of code, this is easy to implement. In the ValidationBase class, add the following code near the end of the Validate method, just before the return statement:

public virtual bool Validate(bool clearBrokenRules)
{
  ...
  foreach (PropertyInfo property in properties)
  {
    ValidationBase vbase = property.GetValue(this, null) as ValidationBase;
    if (vbase != null && !vbase.Validate())
    {
      foreach (BrokenRule brokenRule in vbase.BrokenRules)
      {
        BrokenRules.Add(new BrokenRule(string.Format("{0}.{1}", 
             property.Name, brokenRule.PropertyName), brokenRule.Message));
      }
    }
  }  
  return (BrokenRules.Count == 0);
}

When you run this code, the Address property is now validated as well. The BrokenRules collection of the ContactPerson now also contains broken rules for the address in the format ParentPropertyName.SubPropertyName, ValidationMessage. So, for example for an Address property whose zip code is not valid, you get an error like:

Address.ZipCode, "Enter your zip code"

You can use these validation rules to build up a list in the UI similar to the way I have done in my article series on N-Layer design.

The cool thing about this solution is that it works recursively. If the Address instance in turn contains another ValidationBase property (like LocationOnEarth) you would end up with a broken validation rule like Address.LocationOnEarth.Long, as shown in the following screen shot of the debugger:

Screen shot of the debugger showing the broken validation rules for the nested objects.

Thanks for the tip, Maarten. I'm sure this comes in handy when dealing with more complex object hierarchies.

Download Files

The modified ValidationBase class (C# only)


Where to Next?

Wonder where to go next? You can post a comment on this article.

Doc ID 493
Full URL https://imar.spaanjaars.com/493/extending-validationbase-to-validate-properties-of-type-validationbase
Short cut https://imar.spaanjaars.com/493/
Written by Imar Spaanjaars
Date Posted 04/26/2009 17:35
Listened to when writing Contender by The Pains of Being Pure at Heart (From the album: The Pains of Being Pure at Heart)

Comments

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.

(Plain text only; no HTML or code that looks like HTML or XML. In other words, don't use < and >. Also no links allowed.