VAB: Validating dependent properties on WinForms

Topics: Validation Application Block
Mar 13, 2007 at 8:48 AM

I'm stuck in the following scenario, hopefully someone is able to help me out.

I have several dependent properties (which are textboxes on windows forms). So eg I have a property A which can only have a value of 50 and above when property B has a value of 100 and lower.

So the main problem is to compare properties on a more detailed level than the comparison provider allows me to (which only allows greater than etc).

I tried SelfValidation which works very well on the business level (Validation.ValidateFromConfiguration<Settings>(Settings, "Test")), but using the windows forms integration the SelfValidation functions doesnt seem to get executed. Also I don't know how to add an error message for a specific property.

I also tried to write my own custom validator, but I don't know how to add properties (parameters) to that validator. So I can only validate the value that is associated with that validator, not dependent properties.

Maybe someone knows how to do this - this is probably a pretty common task.

Mar 14, 2007 at 10:57 AM
I like to rephrase my problem as nobody answered to my previous post.

How can I use the VAB to validate complex property dependencies?

Its important that this works with the WindowsForms integration of VAB.

Mar 14, 2007 at 5:40 PM
The Winforms and ASP.NET integration layers work a bit differently to you might expect. Instead of creating a business object, populating the data and validating the object, they actually extract the metadata from the classes and validate the data directly from the controls (ie no business object is created). This means that self validation is not supported, and any custom validators that rely on reflection to find other members will not work.

The good news is that it should still be possible to do what you're describing. The PropertyComparisionValidator does much the same thing - but the solution is a bit more complex as we needed to use pluggable strategies that enable the integration layer to tell the validator how to find the related values. If you follow the same patterns used in that validator you should be OK.

Mar 16, 2007 at 7:29 PM
Hi Tom, I was waiting for this answer since I have the same doubt as testalino, (although I am not much concerned about the WindowsForms integration yet). The thing is that I have my business object filled of validation attributes in almost all of its properties, but the validation of one of them depends on another property's value. Since I didn't like the SelfValidation approach, I created a custom validator (of type int) for my property and obtained the other property’s value using reflection. This approach didn't convince me either, and now that you say it wouldn't work with integration I like it even less.

My custom validator that uses reflection looks more or less than this:

protected override void DoValidate(int objectToValidate, object currentTarget, string key, ValidationResults validationResults){
    //obtain the other property value through reflection
    PropertyInfo prop = currentTarget.GetType().GetProperty("MyOtherPropertyName");
    int myOtherPropValue = prop.GetValue(currentTarget, null) as int;
    //some custom logic here
    if (objectToValidate > myOtherPropValue + 50 ){

I took a look at the PropertyComparisonValidator you mention, but I still don't get how it must be used. I've noticed it takes a "ComparisonOperator" as parameter, but the relation between my properties does not match any of the enum values.

How I should modify my custom validator to access the other property’s value without using reflection?

Thank you in advance,


Mar 16, 2007 at 8:39 PM
Sorry Sole, I agree the code is a little confusing.

The key to finding other members in a integration-friendly way is using the ValueAccess class. If you take a look at the PropertyComparisonValidatorData class, which is responsible for building the PropertyComparisonValidator class, you can see how it creates the appropriate ValueAccess instance:
    return new PropertyComparisonValidator(memberValueAccessBuilder.GetPropertyValueAccess(propertyInfo),
The same code also exists in the PropertyComparisionValidatorAttribute class.

Inside the ValueAccessComparisionValidator class (which is the parent class for PropertyComparisonValidator, in the DoValidate method you can see how the ValueAccess instance is used to find the related property:
bool status = this.valueAccess.GetValue(currentTarget, out comparand, out valueAccessFailureMessage);

You should be able to follow a similar pattern in your validator - make sure it takes a ValueAccess in its constructor, use it to find the related member, and provide a friendly way to create the appropriate ValueAccess value in the configuration and attribute-based creation mechanisms.

Mar 19, 2007 at 10:00 AM
Hello Tom,

the creation of custom validators that work together with the configuration gui is a mystery to me. It would be nice if you could go into more details here.

Basicly, like Sole, I want a MultiplePropertyComparisonValidator and I want to use that validator in the configuration.

When I open the configuration in VS2005 and select "new" on a property I get a list of standard validators and one called "custom validator". This custom validator entry doesnt have any designer-properties (like propertiesToCompareTo), so I don't know how I should integrate my MultiplePropertyComparisonValidator into the designer gui (eg extend the "new validator" list). Maybe you can give me a quickstart or link to a help file that does that, that would help me alot.

Also I would like to point out that this is probably something alot of other users of this application block are interested in (comparing multiple properties without limitation on the comparison operation (GreaterEqual is not sufficient in my example).

I think a general MultiplePropertyComparisonValidator that is integrated in the VAB would be a good idea and maybe considerable on your side. This could be an abstract class that provides the comfort of the configuration integration (eg you could provide a property that takes collection of strings as propertyNames, so you don't limit the number of properties). That base-class could takes those properties and pass ValueAccess objects as a collection (maybe a dictionary of <propertyname as string, valueaccess>) to the derived validator which implements the comparison logic.

What are you thoughts on this subject?

Basicly I want a validator much like the PropertyComparison validator in the VAB that has a configuration-gui property "DependentProperties" which is a collection strings. What would I have to do to implement that validator (just the basic steps) and integrate it into the configuration gui?

Thanks in advance.
Regards, Andreas
Mar 19, 2007 at 5:34 PM
I implemented a MultiplePropertyComparisonValidator base-class today that does what I described above (following a similiar approach as the CustomValidator). However I still couldn't figure out how to extend the configuration-gui without actually changing the source-code of the GUI (I noticed the ValidatorNode classes and tried to fit mine in, but that didnt work too well - I think this isnt the way its supposed to work).
May 16, 2007 at 4:05 PM
Did anyone find a solution to the above? I'm also trying to find a neat way add a new multi comp validator without having to change the source files.
May 16, 2007 at 6:51 PM
I have the code of the custom validator that accesses another property's value using ValueAccess posted in my blog. But I'm afraid I've used attributes to place the validators. Haven't used the ConfigTool. Neither have tried the UI integration.