Design time support for custom validator - Validation Application Block

Topics: Validation Application Block
Apr 16, 2013 at 4:19 AM
I have created Custom validator using Validation Application Block. Its working fine. But how to give design time support for the same validator. I have to use this custom validator in Enterprice Library Configuration Application Console How?

This is Custom Validator Code..

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class StringIsNullOrEmptyAttribute : ValueValidatorAttribute
{
    protected override Validator DoCreateValidator(Type targetType)
    {
        return new StringIsNullOrEmpty(this.MessageTemplate, this.Negated);
    }
}

public class StringIsNullOrEmpty : ValueValidator<string>
{
    public StringIsNullOrEmpty(string messageTemplate, bool negated)
        : base(messageTemplate, null, negated)
    {
    }

    protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
    {
        if (string.IsNullOrEmpty(objectToValidate) != Negated)
        {
            LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);
        }
    }

    protected override string DefaultNegatedMessageTemplate
    {
        get
        {
            return "Field cannot have a value.";
        }
    }

    protected override string DefaultNonNegatedMessageTemplate
    {
        get
        {
            return "Field is required.";
        }
    }
}
Image
Apr 17, 2013 at 7:30 AM
Edited May 1, 2013 at 9:26 PM
You might consider looking at the Enterprise Library 5.0 - Extensibility Labs. There isn't a specific validation scenario but it does have an exercise on plugging into the config tool which could be helpful.

Basically, you should create a configuration class typically called [ValidatorName]Data. So, in your case it would be StringIsNullOrEmptyData (the Enterprise Library validators end with "Validator" so you may want to follow that convention). This class will extend Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValueValidatorData. A simple example would be:
    [ResourceDescription(typeof(DesignResources), "StringIsNullOrEmptyDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "StringIsNullOrEmptyDataDisplayName")]
    public class StringIsNullOrEmptyData : ValueValidatorData
    {
        public StringIsNullOrEmptyData() { Type = typeof(StringIsNullOrEmpty); }

        public StringIsNullOrEmptyData(string name) : base (name, typeof(StringIsNullOrEmpty))
        {
        }

        protected override Validator DoCreateValidator(Type targetType)
        {
            return new StringIsNullOrEmpty(MessageTemplate, Negated);
        }        
    }
Then you need to apply the ConfigurationElementType attribute to the validator:
    [ConfigurationElementType(typeof(StringIsNullOrEmptyData))]
    public class StringIsNullOrEmpty : ValueValidator<string>
    {
        public StringIsNullOrEmpty(string messageTemplate, bool negated)
            : base(messageTemplate, null, negated)
        {
        }
    //...
    }
The final step is to deploy the assembly to the same directory as the configuration tool and now you should have full integration with the configuration tool.

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