Custom Validator help needed.

Topics: General discussion, Validation Application Block
Dec 21, 2008 at 5:09 PM
Hi,
I need to create a custom validator,I am struggling to find some tutorials on how to build them.What to inherit from etcc.

The ones that i have found goggling they all seems to refer to Application Software factory which i beleive is redundant with 2008.

The method that I have written returns a boolean value .
What shall I inherit from Validator ,valueValidtor or....?

Any tutorials anywhere ?
Dec 22, 2008 at 8:27 AM
Hi,

See this tutorial if this helps:

http://aspalliance.com/1231_Working_with_Custom_Validators_using_Enterprise_Library_3.5


Valiant Dudan
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Dec 22, 2008 at 10:59 AM
Thanks for that link ,I  did come across it but didnt help that much
I understand when to use a DomainValidator but not sure when to use a ValueValidator or Validator<T>

I am creating a CardValidator and the idea is that you pass a cardNumber and it should check if is valid or not.

I am implementing  using a config file approach but  the first Constructor gets called and failed as there is nothing is the collection.

Am I missing something?
this is my attempt :

 

 

[ConfigurationElementType(typeof(CustomValidatorData))]
    public class CardValidator : Validator<string>
    {
        private string cardNumber;

        public string CardNumber
        {
            get { return this.cardNumber; }
            set { this.cardNumber = value; }
        }

        public CardValidator(NameValueCollection attributes)
            : base(null, null)
        {
            //TODO CRASHES HERE
           // cardNumber = attributes.Get("CardNumber").ToString();
        }

        public CardValidator(string cardNumber) : this(cardNumber, null, null)
        {
        }
        public CardValidator(string cardNumber, string messageTemplate) : this(cardNumber, messageTemplate, null)
        {
        }

        public CardValidator(string cardNumber, string messageTemplate, string tag) : base(messageTemplate, tag)
        {
            this.cardNumber = cardNumber;
        }

     
        protected override void DoValidate(string objectToValidate,
                                           object currentTarget,
                                           string key,
                                           ValidationResults validationResults)
        {
            bool isValid =Utilities.IsValidCardNumber(objectToValidate.ToString());
            if (!isValid)
            {
                LogValidationResult(validationResults, this.MessageTemplate, currentTarget, key);
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return "The card number {0} is not a valid one"; }
        }
    }
}

 thanks for any suggestions or help

 


Dec 23, 2008 at 12:07 PM
Add members to the Attributes property of your CardValidator in the configuration.  Those will be the contents of the NameValueCollection property


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

Dec 23, 2008 at 12:27 PM
Thanks for your reply. I see that will help.Not sure what value to put there.

Trying to understand the use of it.Are we suppose to put default values there.
What is the purpose of Attributes Collection?
Sorry for silly question

Fantastic support guys !!
Dec 28, 2008 at 9:51 AM
Not actually default but constant values.  If you check out the ValidationQuickStart, there is a sample DivisibleByValidator custom validator class included in there.  The purpose of the validator is to check if a specific number is divisible by ten so what's included in its Attributes collection is "Divisor" with a value of 10.  Hope this gives you an idea.

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