Multi-Object Validation

Topics: Validation Application Block
Apr 20, 2007 at 6:19 PM
I am having trouble with two things regarding validation on multiple objects.

First, I have a class that contains a subclass like so:
public class ModelOne {
  public ModelTwo Model;
  public int Amount;
}
 
public class ModelTwo {
  public int Amount2;
}

I'm using the Configuration Tool to add validation rules instead of using Attributes. I add a Range Validator to ModelOne.Amount, an Object Validator to ModelOne.Model, and a Range Validator to ModelTwo.Amount2. However, the validation results from ModelOne don't seem to valiate ModelTwo. Any ideas?

Secondly, what if ModelTwo.Amount2 needs to be greater than ModelOne.Amount? Does the Property Validator work across objects? I couldn't even test this until I get the secondary validation to work.

I'm kinda stuck (plus I have a couple other questions unanswered in this forum too that are causing me grief).

Thanks in advance,

Sergio
Apr 20, 2007 at 6:30 PM
Hi Sergio -


I'm using the Configuration Tool to add validation rules instead of using Attributes. I add a Range Validator to ModelOne.Amount, an Object Validator to ModelOne.Model, and a Range Validator to ModelTwo.Amount2. However, the validation results from ModelOne don't seem to valiate ModelTwo. Any ideas?


This shoudl work - is your Object Validator referring to the ruleset used to validate ModelTwo by name?


Secondly, what if ModelTwo.Amount2 needs to be greater than ModelOne.Amount? Does the Property Validator work across objects? I couldn't even test this until I get the secondary validation to work.


No this is not supported by the Property Proxy Validator. If you don't care about UI integration, it would be relatively straightforward to build a validator that uses reflection to deal with this situation. The UI integration layers add a lot of complexity to validators that need to find data in other objects.

Tom
Apr 20, 2007 at 6:43 PM

This shoudl work - is your Object Validator referring to the ruleset used to validate ModelTwo by name?


Yes all rulesets are named "Default", and the targetRuleset attribute on the Object Validator is set to "Default" as well.

Do they have to have separate names?


No this is not supported by the Property Proxy Validator. If you don't care about UI integration, it would be relatively straightforward to build a validator that uses reflection to deal with this situation. The UI integration layers add a lot of complexity to validators that need to find data in other objects.


Actually, I've very concerned with UI integration. However, the problem is that we'd like to encapsulate all the rules in one place (and not have separate UI and model rules). It's looking more and more like that might be a futile goal. Also, since we want to use a separate configuration source for validation, we'll have to build our own WinForms integration since the ValidationProvider doesn't seem to support alternative configuration sources.

So, what's best practice for this? Separate UI validation from the model validation?

- Sergio
Apr 20, 2007 at 9:00 PM
The QuickStart demonstrates the Object Validator using both attributes and configuration. Have you checked the QuickStart to see if it works as you would expect and whether you're doing anything different?

Unfortunately we do not support using non-default configuration sources with the WinForms integration, although it should be possible to add support to this by modifying the integration code. Also I would imagine you could build a cross-object flavor of the Property Comparison Validator that was integration-friendly by using the same techniques as in the existing validator, i.e. using the ValueAccess stuff instead of pure reflection.

Tom
Apr 20, 2007 at 11:29 PM

The QuickStart demonstrates the Object Validator using both attributes and configuration. Have you checked the QuickStart to see if it works as you would expect and whether you're doing anything different?


Tom, it's odd. I got it to work in another contrived example. So I must have something stupid going on with the validation configurations. I can't find it, but I'm going to go out on a limb here and say it's me. :-)


Unfortunately we do not support using non-default configuration sources with the WinForms integration, although it should be possible to add support to this by modifying the integration code.


That's unfortunate, but after looking at the ValidationProvider code, I came to the same conclusion...except that I don't think I can modify it to add that support. It's just a tad right of complicated, and the validator creation is buried so far down in the stack that I have yet to find it. But I'm guessing that it would require passing down an IConfigurationSource through tons of classes. Alas, I think it's beyond the scope of my current project, so I'll have a work on another method (I'm thinking of creating an Extender that will link the ValidationResults to the ErrorProvider via the Result key stored in a property on the extendee).


Also I would imagine you could build a cross-object flavor of the Property Comparison Validator that was integration-friendly by using the same techniques as in the existing validator, i.e. using the ValueAccess stuff instead of pure reflection.


I will definitely look into that. While not necessarily scary on its own Reflection plus this Application Block is intimidating to say the least.

Thanks a lot for all your help Tom. I've been reading through these discussion forums, and really appreciate the level of commitment you've personally made to this product. Your dedication and work ethic sets the bar high for not only Microsoft managers, but for all development managers.

Thanks again,
Sergio