Cross Property Validation

Topics: Validation Application Block
Apr 23, 2007 at 11:30 PM
I'm having a bit of a problem trying to figure out how to do cross-property validation. The Property Comparison Validator seems to be limited to comparing numeric data.

What if FieldB was optional unless FieldA had some text in it? How do you do that?

Apr 24, 2007 at 2:54 AM
The Property Comparison Validator isn't limited to numeric data, but it is limited to comparison operators such as = != < > <= and >=. You would need to build your own validator to support this requirement, but you could probably base the implementation heavily off the Property Comparision Validator.

Apr 24, 2007 at 5:08 PM
Thanks Tom. I thought as much, but wanted to be sure.

Another quick question...there's no facility for marking fields required or optional (other than the NotNullValidator), right? And I couldn't find a way to even peek at the validations so we can visually mark fields as required (I think we need something like the ASP.Net required field validator).
Jan 23, 2009 at 4:06 PM

A cross-property custom validator would be very handy. It would be useful if we could run a validator only if some property had some value, allowing to change that rule in the configuration file.
Does anyone have such a validator that could be shared with the community?


Jan 26, 2009 at 7:03 AM

Are you refering about a validator the only validates if a certain property has some values? right? if that is the case, that functionality can be done either by a custom validator or self-validation. I dont have an existing code example but here are some links that can help you get started.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Jan 26, 2009 at 7:44 AM
Thanks for your reply, Valiant.

Yes, it's exactly what I meant, a validator that only validates if a certain property has some values.
However self-validation is too rigid because it doesn't allow runtime changes. The challenge would be to create a custom validator that could be changed in the configuration file and that would be sufficiently generic, something like a validator that would use regular expressions to evaluate the running condition and the actual validation. 

Thank you,


Jan 27, 2009 at 2:05 AM
Here's some thoughts, you might be able to create your own validator.  If you want to validate based on values of other properties, then the whole entity you're validating should be passed to your custom validator.  What I'm thinking is for example, if you have a class "Employee", your validator could inherit from Validator<Employee>.  Your Employee class then could be wrapped in another class, make it a property of that class so you could specify the validator you just created.  You may say that it's just like creating a SelfValidation method so I'm also thinking maybe you could resort to the Attributes property of the validator since you can configure it in the editor.  It's up to you how you're going to use it to make your validation sort of dependent on the values of that collection.  That's it.  That's what I can come up, but it may not be as generic as how you would like it to be though.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.