Format UI according to Validation Rules

Topics: General discussion, Validation Application Block
Apr 2, 2009 at 11:29 PM

Is it possible to format textboxes according to certain rules defined in different ruleset's?

For example:

object Person (Name, Address, Email, PhoneNr)
RuleSetEmployee(Validates Name as mandatory, PhoneNr with RegExp and Mandatory, but Email shouldn't be required)
RuleSetCustomer (All fields are mandatory with regular expression in e-mail and PhoneNr)

The Web UI used to collect information is the same, although i send a parameter by querystring, that indicates that we're talking about a customer or a employee. If I want to add a new employee, I want to be capable to disable the Email TextBox, because it is not needed in the Employee record.

Is it possible? I've thought in a possible way, but don't know if it is possible. In the aspx page, in some manner access the xml file with the validation Rules, to check if a given property of a ruleset has validators, and if it hasn't, apply a different css style to the textbox control. Is it possible? Are there any method to access the defined ruleset's?

Best regards,
Apr 3, 2009 at 8:48 AM
You can use this code:

Keep in mind that you use SystemConfigurationSource if your validation rules are in the default configuration file (app.config/web.config).   The Types property of the ValidationSettings class lists the types defined in the config with validation rules.  The Rulesets lists the rule sets for each type.  For each rule set, aside from the Properties with attached validators, you can also examine the fields as well as the methods.  

IConfigurationSource configSource = new SystemConfigurationSource();
ValidationSettings settings = (ValidationSettings)configSource.GetSection("validation");
ValidatedTypeReference typeReference = settings.Types.Get(typeof(Foo).FullName);
foreach (ValidationRulesetData ruleSet in typeReference.Rulesets)
if (ruleSet.Properties.Contains("PropertyName"))
        //apply css


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
Apr 3, 2009 at 10:02 PM
Hi Sarah,

Thank you for you suggestion, but didn't solve my problem, because i don't have my ruleset defined in the Web.config.

I have the following scenário:

-> MyProject-WebApp (with reference to the MyProject-BO project)

It is in MyProject-BO that i have - in the file MyProject-BO.dll.config - the ruleset's definition.

Is it possible to do something similar to what you suggested above, but with my approach/architecture?

Best Regards,
Apr 4, 2009 at 1:45 AM
Yes just create an instance of FileConfigurationSouce passing in the filename of your dll.config instead of creating a SystemConfigurationSource.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.