How would you do this with the VAB?

Topics: Validation Application Block
Apr 24, 2007 at 8:17 PM
Edited Apr 24, 2007 at 8:17 PM
Using th VAB, how would you validate for the possible condition

field can be null
field can be empty string
field can be between 1 - 20 characters
field must not contain characters 'pobox'

my thoughts were

        [ValidatorComposition(CompositionType.Or)]
        [NotNullValidator(Negated = true)]
        [StringLengthValidator(0, RangeBoundaryType.Inclusive, 0, RangeBoundaryType.Inclusive)]
        [ValidatorComposition(CompositionType.And)]
        [ContainsCharactersValidator("pobox", ContainsCharacters.All, Negated = true)]
        [StringLengthValidator(1,20)]

this does not work

anyone any further ideas?
Apr 24, 2007 at 9:17 PM
Unfortanately it isn't possible to combine AND and OR composition when using attributes. This is due to the limited expressiveness of the attribute syntax, including the lack of guarantee of the order in which the attributes will be discovered via reflection.

If you use configuration to define the rules, you could do this by setting up the following hierarchy:
  • Contains Characters Validator
  • Or Composite Validator
    • Not Null Validator (Negated)
    • String Length Validator

Tom
Apr 25, 2007 at 7:49 AM

tomhollander wrote:
Unfortanately it isn't possible to combine AND and OR composition when using attributes. This is due to the limited expressiveness of the attribute syntax, including the lack of guarantee of the order in which the attributes will be discovered via reflection.

If you use configuration to define the rules, you could do this by setting up the following hierarchy:
  • Contains Characters Validator
  • Or Composite Validator
    • Not Null Validator (Negated)
    • String Length Validator

Tom


Thanks for the post Tom.

Would it be a possibility in the future to tag the rules with an ID relating to the order in which they should be applied (i.e. a sequence number) for instances such as this, or is this not workable?