Constructor on type Validators.RequiredStringValidator' not found

Topics: Building and extending application blocks, Validation Application Block
Jun 21, 2010 at 12:39 PM

I am using this http://geekswithblogs.net/michelotti/archive/2008/06/12/122836.aspx below code.

public class RequiredStringValidatorAttribute : ValueValidatorAttribute

   2: {
   3:    protected override Validator DoCreateValidator(Type targetType)
   4:    {
   5:        return new RequiredStringValidator(this.MessageTemplate, this.Negated);
   6:    }
   7: }
   8:  
   9: public class RequiredStringValidator : ValueValidator<string>
 10: {
 11:    public RequiredStringValidator(string messageTemplate, bool negated) : base(messageTemplate, null, negated)
 12:    {
 13:    }
 14:    
 15:    protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
 16:    {
 17:        if (string.IsNullOrEmpty(objectToValidate) != Negated)
 18:        {
 19:            LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);
  20:        }
 21:    }
 22:  
 23:    protected override string DefaultNegatedMessageTemplate
 24:    {
 25:        get
 26:        {
 27:            return "Field cannot have a value.";
 28:        }
 29:    }
 30:  
 31:    protected override string DefaultNonNegatedMessageTemplate
 32:    {
 33:        get
 34:        {
 35:            return "Field is required.";
 36:        }
 37:    }
 38: 

When I set this validator in my confguration 

 

<validator  type="Validators.RequiredStringValidator, Validators, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"  MessageTemplate="Thruconfig"  Negated="false"  name="Required String Validator">

  

:I get the Following  exception in 

Validator validator = (Validator)Activator.CreateInstance(this.Type, this.Attributes); in class class  CustomValidatorData method override Validator DoCreateValidator(Type targetType)

System.MissingMethodException was unhandled by user code

Message="Constructor on type Validators.RequiredStringValidator' not found."

Source="mscorlib"

Please let me know why it is throwing an exception? something incorrectly done? Thanks

Jun 22, 2010 at 4:40 AM

You'll need to add a constructor in your Custom Validator class that accepts a NameValueCollection parameter. Try to add the following lines and see if it works for you.  

        public RequiredStringValidator(NameValueCollection attributes)
            : base(string.Empty, String.Empty, false)
        {
        }

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 23, 2010 at 1:21 PM

Thanks for your help..I found this solution earlier and fixed it. Thats the constructor expected for ValueValidators..

I have one more query on SetInterceptor ..Last one at http://unity.codeplex.com/Thread/View.aspx?ThreadId=206877. If you have any solution for this please let me know.