simple custom validator

Topics: Validation Application Block
Dec 14, 2010 at 2:46 PM

i am playing with validation block and i tried to make a custom validator which validates integers. i created this class.

 [ConfigurationElementType(typeof(CustomValidatorData))]
  public  class IntegerValidator:Validator<int>
    {

       

      public IntegerValidator():base(string.Empty,string.Empty)
      {

      }


        protected override void DoValidate(int objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate <= 0)
            {

                LogValidationResult(validationResults, DefaultMessageTemplate, currentTarget, key);
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return "Not suitable..."; }
        }
    }
but at this line  productValidator = factory.CreateValidator<Product>("ProductRuleSet"); 
i get this error; Constructor on type 'CustomValidators.IntegerValidator' not found.
ofcourse i confugured the block. what i am missing ???
Dec 14, 2010 at 3:32 PM

For VAB to be able to construct your custom validator that you registered in a configuration file, you need to define a constructor overload on that validator that takes a NameValueCollection argument:

public IntegerValidator(NameValueCollection attributes)
    : base(string.Empty, string.Empty)
{
}