Ruleset scope

Topics: Validation Application Block
Oct 4, 2007 at 4:52 PM
What is the scope of a validation with no Ruleset specified?

It reads like they belong to an "anon" ruleset by default.

Sometimes I would like a rule with no ruleset to belong to ALL rulesets, so they get evaluated for every ruleset.

Is there any way of doing this so that I don't have to duplicate every rule for every ruleset on every property!

Cliff
Oct 6, 2007 at 1:14 PM
Hi,

This feature is not provided directly. You can work around this with a trick like this though:

public class DomainClass
{
private int field;

[RangeValidator(0, RangeBoundaryType.Inclusive, 0, RangeBoundaryType.Ignore, Ruleset = "default")]
[RangeValidator(0, RangeBoundaryType.Ignore, 10, RangeBoundaryType.Inclusive, Ruleset = "strict")]
public int Property
{
get { return field; }
set { field = value; }
}

[ObjectValidator("default")]
[ObjectValidator("default", Ruleset = "strict")]
public DomainClass Self
{
get { return this; }
}
}

It's not a great solution, but should work. You will need to declare an ObjectValidator for each ruleset, but you wouldn't need to duplicate the actual rules.

Fernando
Oct 8, 2007 at 10:52 AM
Fernando

Sorry I'm being a bit dim, how does that work and how would I use it?

I have a class with 20 properties and 2 rulesets. On 18 of them the attributes are identical for both rulesets and on two of them they differ for each ruleset.

Cliff
Oct 8, 2007 at 11:11 AM
Hi Cliff,

I'm sorry about the terseness of my reply. The idea is that you would define a ruleset with all the for all the shared validations; I called it "default" here.

The for each of the real rulesets, you would include an ObjectValidator configured for the "default" ruleset on the Self property. That would make all the validations in the "default" ruleset to be applied.

Hope this helps,
Fernando