Validation Application Block - type Validators & configuration

Topics: Validation Application Block
Jun 24, 2008 at 7:50 AM
Edited Jun 24, 2008 at 7:51 AM
Hi guys,

Since a short time i'm using the validation application block. Sometimes i need type validators to validate the hole type and to have access on every property.
This is such a type validator:


    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class UserValidator : Validator<User>
    {
        public UserValidator(string messageTemplate, string tag) : base(messageTemplate, tag) { }
 
        public UserValidator(NameValueCollection attributes) : base(null, null) { }
 
        protected override void DoValidate(User objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            validationResults.AddResult(new ValidationResult(this.DefaultMessageTemplate, objectToValidate, "", "", this));
        }
 
        protected override string DefaultMessageTemplate
        {
            get { return "User validation failed!"; }
        }
    }

Yes, of course it's a dummy implementation, but it should suffice to test.
If i create this validator directly Validator<User> validator = new UserValidator(); Everything works fine and the validation works. But if I define this validator over the configuration with the entlib configuration tool, this validator is never loaded. Even the msdn says explicit

validator Element
The validator element is a child of the ruleset, field, method, and property elements. If part of a composite validator, it can be the child of another validator element. This element specifies a validator that validates the type, field, method, or property. All validators have a name attribute that specifies the name of the validator, a type attribute that defines the type of the validator, and other attributes that are specific to each validator. For more information on the properties for each type of validator, see
Using the Provided Validators.

So this should be possible (generated by the configuration tool):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <validation>
    <type assemblyName="DomainModel, Version=0.6.806.64, Culture=neutral, PublicKeyToken=550e2a18a6ab1f36"
      name="DomainModel.User">
      <ruleset name="Rule Set">
        <validator messageTemplate="" messageTemplateResourceName=""
          messageTemplateResourceType="" tag="" type="Validators.UserValidator, Validators, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
          name="Custom Validator" />
      </ruleset>
    </type>
  </validation>
</configuration>

Sadly i found nobody with the same issue....?
kind regards

laurin
Jun 24, 2008 at 11:20 AM
okey, i found the problem. you need to set the default rule set - otherwise nothing will be loaded. bad validation of the entlib tool. i needed about 2 hours to debug....

kind regards