0

I am using Microsoft Enterprise Library 5.0 Validation Application Block.

I want override the StringLengthValidator which will have only one minor change.

The current StringLengthValidator is fine but the problem is this validator is doing two jobs., NotNull check and String length check. If the string is null then the validator is firing message even if I ignore the lower bound checking.

What I want is if I provide a null value the validator should not fire when I ignore the lowerbound checking, only it should consider upperBound.

Thanks in advance

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sukesh Chand
  • 2,339
  • 2
  • 21
  • 29

1 Answers1

0

You can make use of Or Composite Validator that comes with Enterprise Library. You can have Not Null Validator and String Length Validator to perform your validation.

Here's a quick sample:

[ConfigurationElementType(typeof(CustomValidatorData))] public sealed class CustomValidatorClass : Validator {

    public CustomValidatorClass(string template, string tag)
        : base(template, tag)
    {

    }

    protected override string DefaultMessageTemplate
    {
        get { return "blah blah"; }
    }

    public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
    {
        //Do something here
    }
}

In the configuration file:

<validation>
    <type name="ConsoleApplication1.PropertyClass" assemblyName="ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <ruleset name="Validation Ruleset">
            <properties>
                <property name="MyProperty">
                    <validator type="ConsoleApplication1.MyClass, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                        messageTemplate="Blah blah" name="MyClass" />
                </property>
            </properties>
        </ruleset>
    </type>
</validation>

You can use the generic class for any specific type you want to validate or any of the classes that already extend Validator class in Enterprise Library API.

danish
  • 5,550
  • 2
  • 25
  • 28
  • Thanks for the replay. But every time I have to use two validators. I am looking for a permanent solution like creating a CustomStringLengthValidator and I like to learn more on making custom validators. – Sukesh Chand Apr 16 '13 at 09:19
  • You can create your own validator for this purpose by extending ValueValidator class. – danish Apr 16 '13 at 09:24
  • well...but in the above example how do I pass the MaxLength which is required to validate against the length of string. – Sukesh Chand Apr 16 '13 at 10:29
  • You need to work your way through it. I am not going to write code for you. At least not for free. :) – danish Apr 16 '13 at 10:32
  • @SukeshChand, take a look at [Hands-On Labs for Microsoft Enterprise Library 5.0](http://www.microsoft.com/en-us/download/details.aspx?id=6932). There are exercises for creating custom validators. – Randy Levy May 19 '13 at 20:06