Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

ASP.NET Validation with the Enterprise Library Validation Block

The Enterprise Library comes with a custom ASP.NET validator, that gets validation from the metadata defined in your classes. If you have seen my previous post, you know how to add validation metadata to a POCO, usually a data transfer object. Basically, you either 1) add attributes to public properties and fields or 2) configure XML files. I'll stick to the first approach. What I'll show you next is how to do validation on web forms based on the validation metadata from a class. First, add a reference to the Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet assembly, and enter the following markup on you ASPX page:

<asp:TextBox runat="server" ID="text" />

<entlib:PropertyProxyValidator runat="server" ControlToValidate="text" SourceTypeName="SomeRequest, SomeAssembly" PropertyName="Text" />

<asp:Button runat="server" ID="button" CausesValidation="true" Text="Click Me" />

And that's it! The PropertyProxyValidator, in this case, will check the NotNullValidatorAttribute, StringLengthValidatorAttribute and ContainsCharactersValidatorAttribute that are applied to the Name property of the SomeRequest class.

The available validator attributes on assembly Microsoft.Practices.EnterpriseLibrary.Validation are:

- ContainsCharactersAttribute: Performs validation on strings by verifying if it contains a character set using the ContainsCharacters mode.

- DateTimeRangeValidatorAttribute: Performs validation on DateTime instances by comparing them to the specified boundaries.

- DomainValidatorAttribute: Validates an object by checking if it belongs to a set.

- EnumConversionValidatorAttribute: Validates a string by checking it represents a value for a given enum type.

- HasSelfValidationAttribute: Indicates the target type defines self validation methods.  

- IgnoreNullsAttribute: Indicates that a null value is to be allowed by the validator represented by the validation attributes for the language element this attribute is bound.

- NotNullValidatorAttribute: Logs a failure when validating a null reference.

- ObjectCollectionValidatorAttribute: Performs validation on collection objects by applying the validation rules specified for a supplied type to its members.

- ObjectValidatorAttribute: Performs validation on objects by applying the validation rules specified for a supplied type.

- PropertyComparisonValidatorAttribute: Performs validation by comparing the a value with the value of a property on the target object by using a specified comparison operation.

- RangeValidatorAttribute: Performs validation on instances by comparing them to the specified boundaries.

- RegexValidatorAttribute: Performs validation on strings by matching them to a Regex.

- RelativeDateTimeValidatorAttribute: Validates a DateTime value by checking it belongs to a range relative to the current date.

- SelfValidationAttribute: Marks a method as implementing self validation logic. Used in conjunction with HasSelfValidationAttribute.

- StringLengthValidatorAttribute: Performs validation on strings by comparing their lengths to the specified boundaries.

- TypeConversionValidatorAttribute: Validates a string by checking it represents a value for a given type.

- ValidatorCompositeAttribute: Indicates that the kind of composition to use when multiple ValidatorAttribute instances are bound to a language element.

It is very easy to define your own validators: just inherit from BaseValidationAttribute and implement your own validation logic.

The only drawback is that the page is always posted, that is, there is no JavaScript client validation, but you can place this inside an UpdatePanel, and it works just fine.

Comments

Darrin Maidlow said:

Very helpful, thanks!  I had strayed down a completely incorrect path trying to manually create asp Validation events manually using the Error property on our DTOs.  Using the proper tool saved my huge grief.  :)

# July 24, 2012 12:46 PM

Ricardo Peres said:

@Darrin:

Great! Do let me know if you have problems with that.

# July 24, 2012 1:23 PM