enterprise library 5.0 validation block always return true

May 18, 2010 at 3:36 PM

For some reason , Validation Block always return true when I am trying to run a RuleSet against a Customer Object and the RuleSet is defined in configuration file. Below is console application and the app.config for same. I tried different ways using Validation.Validate, ValidationFactory etc... What am I doing wrong?

         static void Main(string[] args)
        {
            Customer myCustomer = new Customer();
            myCustomer.FirstName = "a";
            //ValidationResults results = Validation.ValidateFromConfiguration<Customer>(myCustomer, "RuleA");  
            //Validator customerValidator = ValidationFactory.CreateValidator<ConsoleApplication2.Customer>("RuleA");
            Validator customerValidator = ValidationFactory.CreateValidatorFromConfiguration(typeof(Customer), "RuleA");
            ValidationResults results = customerValidator.Validate(myCustomer);
            if (!results.IsValid)
            {
                foreach (ValidationResult r in results)
                {
                    Console.WriteLine(r.Message);
                }
            }
            else
            {
                Console.WriteLine("Success");
            }

App.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
<validation>
  <type name="ConsoleApplication2.Customer" defaultRuleset="RuleA" assemblyName="ConsoleApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
    <ruleset name="RuleA">
      <fields>
        <field name="FirstName">
          <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            upperBound="10" lowerBound="5" lowerBoundType="Inclusive" messageTemplate="FirstName should be between 10 and 100"
            messageTemplateResourceType="" tag="FirstName" name="String Length Validator" />
        </field>
      </fields>
    </ruleset>
  </type>
</validation>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
 

Jun 13, 2012 at 9:33 AM

I am having the exact same issue, did you ever solve this?