Creating Validation Rules Programmatically

Topics: Validation Application Block
Oct 14, 2008 at 10:39 AM
I am working on a scenario where I should be able to create validation rules programmatically, assign them to properties of certain classes programmatically and then execute the validation programmatically as well.

I know that I can create simple email validation using the following lines of code:

RegexValidator emailValidator = new RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

I would now want to assign this to a Contact class that has an EmailAddress property programmatically and then call Validation.

What steps can I take to accomplish this? It looks like I should be able to do:
Validator<Contact> parentValidator = ValidationFactory.CreateValidator<Contact>();

But the question is how do I tie up the emailValidator to the EmailAddress property. I know if I manually add the RegEx attribute over the EmailAddress property:

public string EmailAddress { get; set; }

I can setup a RuleSet="Test" and then invoke the same Validation Factory using:
Validator<Contact> parentValidator = ValidationFactory.CreateValidator<Contact>("Test"); and it will be able to execute the validation. However, I cannot find of a way to dynamically perform this validation using programming.

Any help would be great!
Oct 14, 2008 at 12:17 PM
What is the nature of your application, why can't you set up the validation rules using ruleset and call the appropriate ruleset instead of creating validators programmatically?   I think it'll be better.
I'm not sure if you can hook up properties to a specific validator during runtime except of course if you use rulesets, there's no need to do that.  Anyway, hope this helps: 
    Validator<string> validator = new RegexValidator("@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
     ValidationResults results = validator.Validate(contact.EmailAddress);
Oct 14, 2008 at 6:39 PM
Thanks for the reply. I was hoping it would let me do that. The propertyproxyvalidator allows something very similar but requires a control to be passed.
I was wondering if there was a way.
Anyways, the nature of my application is pretty dynamic. The above was just an example question. I would like to dynamically based on the type of data, create validation rules from the UI layer and then tie them to properties on the fly.
For e.g. I only want email validations to fire if the contact belongs to a certain category. I may want to run an altogether diff validation for e.g. the email address should end with for a particular type of contact.

The good thing about these validators is that I don't have to write them from scratch and the rules can be programmed by an end user dynamically from the UI...if I could just associate them dynamically with a property, it would really make things simple.

For e.g. the Spring framework allows creating validators and associating them as shown:
UserInfo userInfo = new UserInfo();  // has Name and Password props

ValidatorGroup userInfoValidator = new ValidatorGroup();

.Add(new RequiredValidator("Name", null));

.Add(new RequiredValidator("Password", null));

ValidationErrors errors = new ValidationErrors();
bool userInfoIsValid = userInfoValidator.Validate(userInfo, errors);
I hope this makes sense. Thank you again for the prompt response.

Anup Marwadi