Adding different validation results based on ruleset/validator in single validation block

Topics: Validation Application Block
Aug 18, 2009 at 3:53 PM

Hi all,

I am in a situation where in I have same set of validation rules for customer and owner. But, I need to display error messages differently. I can write separate validation block with same rules and execute them with the help of rule sets.

 But, I want to minimize lines of code such that single validation block can be used for both the validators.


Here, Customer class has been inherited from Owner class. I have a validation block ValidateMyDates(ValidationResults results) in Owner.cs file which would be executed by two validators v1 andv2 as below.


 Customer myCustomer = new Customer( /* ... */ );

Validator<customer /> v1 = ValidationFactory.CreateValidator<customer />(CustomerRuleset”);

ValidationResults r1 = v1.Validate(myCustomer);


Owner myOwner = new Owner( /* ... */ );
Validator< Owner /> v2 = ValidationFactory.CreateValidator< Owner />(“OwnerRuleset”);           
ValidationResults r2 = v2.Validate(myOwner);


 Owner.cs file is having below validation block.

[SelfValidation(Ruleset = “CustomerRuleset”)]

[SelfValidation(Ruleset = “OwnerRuleset”)]

public virtual void ValidateMyDates(ValidationResults results)


     // ***** Here, I want to validate my dates and add validation results based on ruleset/validator.


I want to add validation results (error messages) based on ruleset or validator. Can any one suggest me to achieve this? Thank you in advance.




Aug 19, 2009 at 1:55 AM

Hi Kumar, in the sample code you posted above, either v1 and v2 executes, there is only a single validator using a single ruleset executing within the ValidateMyDates.  Unless you create a validator passing the two rulesets.  But in that scenario, there is only a single instance of the ValidationResults, there is no distinction between the rulesets and that copy of the validationresults eventually is what you'll get as the return object in the call to Validate.  You would have to create the separate self validation method in the Customer class. 


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.