Validators stopped working after upgrade from 4.1 to 5.0

Topics: Validation Application Block
Dec 13, 2012 at 5:07 PM

We have an interface entity that uses validators on each property.  Here is the declaration of the interface and the first property with validators:

 

    public interface IMemberEntity
    {
        [NotNullValidator(Ruleset = ValidationRulesets.NullValidation, MessageTemplate = "Address1 is required")]
        [StringLengthValidator(1, 100, Ruleset = ValidationRulesets.LengthValidation)]
        string Address1 { get; set; }
}

 

We have several properties in this interface class with these types of validators that were functioning appropriately with Enterprise Library 4.1.  We have several rulesets and we are calling the validate method like so:

 

  var result = Validation.Validate<IMemberEntity>(member, ruleSet);

Since upgrading to 5.0, these validators no longer seem to be functioning.  Has anyone run into this before or have any ideas why this might be happening?

Dec 14, 2012 at 4:10 AM

There was a change in behavior between version 4 and 5 (not sure if it was intentional).  The workaround is to use the CreateValidator method:

ValidationResults r = ValidationFactory.CreateValidator<IMemberEntity>(ruleSet).Validate(member);

--
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to 

Dec 19, 2012 at 7:32 PM

That appeared to fix some of the issues, but not all of them.  For example, the NotNullValidator above still does not function.  Any other ideas for correcting this?

Dec 19, 2012 at 8:41 PM

The NullValidator is using a different ruleset name so you would need to validate against that ruleset:

    var nullValidator = ValidationFactory.CreateValidator<IMemberEntity>(ValidationRulesets.NullValidation);
    ValidationResults validationResults = nullValidator.Validate(member);

Or you can validate against multiple rulesets:

    var nullValidator = ValidationFactory.CreateValidator<IMemberEntity>(ValidationRulesets.NullValidation);
    ValidationResults validationResults = nullValidator.Validate(member);

    var lengthValidator = ValidationFactory.CreateValidator<IMemberEntity>(ValidationRulesets.LengthValidation);
    lengthValidator.Validate(member, validationResults);

Or you could remove the ruleset name and just validate:

    interface IMemberEntity
    {
        [StringLengthValidator(1, 100)]
        [NotNullValidator(MessageTemplate = "Address1 is required")]
        string Address1 { get; set; }
    }


    var validator = ValidationFactory.CreateValidator<IMemberEntity>();
    ValidationResults validationResults = validator.Validate(member);

Also the StringLengthValidator(1, 100) will fail on a null so you might want to just use one validator and message:

    interface IMemberEntity
    {
        [StringLengthValidator(1, 100, MessageTemplate="Address1 is required and must be between 1 and 100 characters.")]
        string Address1 { get; set; }
    }

--
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to