Working with Custom Validators using Enterprise Library 3
page 2 of 8
by Brian Mains
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33542/ 82

Validation Overview

For this to work using the attribute approach as discussed previously (where each validator is defined as an attribute of the property it will validate), the validator entity requires two classes. First, it requires the interface of the actual validator itself, which inherits from the several base classes available. Secondly, it requires the creation of an attribute class (inherited from ValidatorAttribute), which handles exposing properties through the constructor and the creation of the validator class.  To create a custom validator, there are several classes you can inherit from, and the ones I will illustrate are the Validator, ValueValidator, and DomainValidator classes.  These are all generic implementations, but there are non-generic versions as well.

Validators use the DoValidate method to perform validation, which passes a reference to the value being evaluated, the source control, a key representing the property being validated, and a collection object that every validation result can be added to. With the generic implementation, the object being validated is of the correct generic type, making this method more type-specific.  In addition to overriding the DoValidate method, a default message property is usually required.  The base Validator class has a single method, whereas the ValueValidator has a non-negated and a negated version of the default message. The DomainValidator class handles these methods so you do not need to override it; you just need to provide the data at constructor time.

View Entire Article

User Comments

Title: mmm Reply   
Name: Brian
Date: 2008-06-23 10:46:18 AM
Validator doesn't inherit from that class, correct. You have to create an accompying attribute that works along side of it. Create another class that inherits from ValidatorAttribute (the attribute class). It has a method you need to override that uses your validator to validate the data. Sorry, I didn't include it in the article.
Title: mmm   
Name: John
Date: 2008-06-21 11:58:45 PM
Base class Validator don't inherit System.Attribute. How can i use it in this way:
[StringLengthValidator(7, RangeBoundaryType.Inclusive, 150,
"The email address must be between 7 and 150 characters",
Ruleset = "primary")
, ContainsCharactersValidator("@.", ContainsCharacters.All,
"The email must have an @ and at least one period",
Ruleset = "primary"),
EmailDomainValidator(".com", ".net", ".edu", ".gov", ".biz", ".tv",
Ruleset =

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-12-08 2:30:01 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search