Nested validations in attributes

Topics: Validation Application Block
Jul 10, 2008 at 4:23 PM

Hi all,

We have a problem with using nested validators as attributes. When I create a certain rule in the configuration file it works fine, but we need to have it as an attribute.

Our rule is basicly like this:
<andValidator>
   <validator1 messageTemplate="message1"/>
   <orValidator messageTemplate="message2">
      <validator2 />
      <validator3 />
   </orValidator>
</andValidator

so it has 2 requirments:
1 : when validator 1 failt it should throws message1
2: when validator 2 or 3 fails it should throw just 1 message -> message2.

We managed to build requirment 1 and 2 seperatly without a problem. Example
1: [ContainsCharactersValidator("test",Negated=true, MessageTemplate="Message1"]
2:     [NotNullValidator(Negated = true),
    ValidatorComposition(CompositionType.Or, MessageTemplate = "Message2"),
    StringLengthValidator(5)]

but we just can't combine them.
If 1 is placed before or after 2, only message2 will fire, we cant get our message1.
If we use an And operator on various ways it will fire a message for any validation which fails.

Is there any way to resolve this?
 

Jul 11, 2008 at 10:38 PM

Hi,

It's hard in general to represent such complex, multi-level structures with attributes, and you cannot do it in general. If you own all the involved types, which is an unlikely situation, you can take advantage of the ObjectValidatorAttribute to accomplish what you're asking in a less than optimal way like this:

    public class Foo

    {

        [ValidatorComposition(CompositionType.And)]

        [Validator1]

        [ObjectValidator(Ruleset="kludgy")]

        public Bar MyProperty { get; set; }

    }

 

    [ValidatorComposition(CompositionType.Or, Ruleset="kludgy")]

    [Validator2(Ruleset = "kludgy")]

    [Validator3(Ruleset = "kludgy")]

    public class Bar

    {

    }

Fernando

 


NicoVenema wrote:

Hi all,

We have a problem with using nested validators as attributes. When I create a certain rule in the configuration file it works fine, but we need to have it as an attribute.

Our rule is basicly like this:
<andValidator>
   <validator1 messageTemplate="message1"/>
   <orValidator messageTemplate="message2">
      <validator2 />
      <validator3 />
   </orValidator>
</andValidator

so it has 2 requirments:
1 : when validator 1 failt it should throws message1
2: when validator 2 or 3 fails it should throw just 1 message -> message2.

We managed to build requirment 1 and 2 seperatly without a problem. Example
1: [ContainsCharactersValidator("test",Negated=true, MessageTemplate="Message1"]
2:     [NotNullValidator(Negated = true),
    ValidatorComposition(CompositionType.Or, MessageTemplate = "Message2"),
    StringLengthValidator(5)]

but we just can't combine them.
If 1 is placed before or after 2, only message2 will fire, we cant get our message1.
If we use an And operator on various ways it will fire a message for any validation which fails.

Is there any way to resolve this?
 




Jul 12, 2008 at 10:24 AM

Hi,

Thank you for your detailed answer. I think we better fall back to a custom validator.

Regards,

Nico


fsimonazzi wrote:

Hi,

It's hard in general to represent such complex, multi-level structures with attributes, and you cannot do it in general. If you own all the involved types, which is an unlikely situation, you can take advantage of the ObjectValidatorAttribute to accomplish what you're asking in a less than optimal way like this:

    public class Foo

    {

        [ValidatorComposition(CompositionType.And)]

        [Validator1]

        [ObjectValidator(Ruleset="kludgy")]

        public Bar MyProperty { get; set; }

    }

 

    [ValidatorComposition(CompositionType.Or, Ruleset="kludgy")]

    [Validator2(Ruleset = "kludgy")]

    [Validator3(Ruleset = "kludgy")]

    public class Bar

    {

    }

Fernando

 


NicoVenema wrote:

Hi all,

We have a problem with using nested validators as attributes. When I create a certain rule in the configuration file it works fine, but we need to have it as an attribute.

Our rule is basicly like this:
<andValidator>
   <validator1 messageTemplate="message1"/>
   <orValidator messageTemplate="message2">
      <validator2 />
      <validator3 />
   </orValidator>
</andValidator

so it has 2 requirments:
1 : when validator 1 failt it should throws message1
2: when validator 2 or 3 fails it should throw just 1 message -> message2.

We managed to build requirment 1 and 2 seperatly without a problem. Example
1: [ContainsCharactersValidator("test",Negated=true, MessageTemplate="Message1"]
2:     [NotNullValidator(Negated = true),
    ValidatorComposition(CompositionType.Or, MessageTemplate = "Message2"),
    StringLengthValidator(5)]

but we just can't combine them.
If 1 is placed before or after 2, only message2 will fire, we cant get our message1.
If we use an And operator on various ways it will fire a message for any validation which fails.

Is there any way to resolve this?