Can't get this simple validation working

Topics: Validation Application Block
May 31, 2010 at 12:08 PM

No matter what I try, the validatorResults object contains no validation messages. Can someone tell me what I'm doing wrong. Thanks.

 

<?xml version="1.0" encoding="utf-8" ?>
<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="ValidationTest1.ChapterName" assemblyName="ValidationTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
            <ruleset name="Validation Ruleset">
                <add type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    upperBound="100" lowerBound="1" lowerBoundType="Inclusive"
                    messageTemplate="qeqwewqe" name="String Length Validator" />
            </ruleset>
        </type>
    </validation>
</configuration>

 

 

Here is my code

============

 

class Program {

        static void Main(string[] args) {

            var config = new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource(@"<pathToFile>\ValRules.config");

            IServiceLocator iservice = EnterpriseLibraryContainer.CreateDefaultContainer(config);

            ValidatorFactory vfactory = iservice.GetInstance<ValidatorFactory>();

            var chapterNameValidator = vfactory.CreateValidator<ChapterName>();

            var validatorResults = new ValidationResults();

            var cn = new ChapterName {
                Title = new String('x', 201)
            };

            chapterNameValidator.Validate(cn, validatorResults);
        }
    }

    public class ChapterName {

        public string Title { get; set; }
    }

 

 

 

May 31, 2010 at 3:14 PM
Edited May 31, 2010 at 3:15 PM

Try including the upperBoundType:

<add type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
upperBound="100" lowerBound="1" lowerBoundType="Inclusive" upperBoundType="Inclusive"
messageTemplate="qeqwewqe" name="String Length Validator" />

Jun 1, 2010 at 12:08 AM

And make sure to set the defaultRuleSet.

<type name="ValidationTest1.ChapterName" defaultRuleset="Validation Ruleset" assemblyName="ValidationTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Jun 1, 2010 at 10:34 AM

This worries me as I used the GUI to create the rules. Seems it didn't enter those attributes in.

Thanks for your time anyway, much appreciated.

Jun 2, 2010 at 12:28 AM

You need to set explicitly the Default Ruleset property in the config tool so that the attribute will appear in the xml editor.  I believe your validation would work even if you didn't set explicitly the upperBoundType attribute in the xml editor, as long as you specified the default rule set.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com