What are we facing?
I want to add validation to an existing ASP.NET application. The business entities used by the application are defined on a separate assembly, used by other desktop applications. The validation mechanism provided by ASP.NET is not enough because:
- I want to easily add my own customized, complex validation rules.
- I’d like to avoid defining the same rules on both the desktop applications and the web applications.
The solution
You can use the Validation Application Block (VAP) included in the version 4.0 of Enterprise Library. When using the VAP you decorate your business entities with attributes indicating the validation performed against their properties. For instance, you might have a class Person which defines, among other members, the property Age as an integer number between 21 and 100.
In order to specify the constraint just described with VAP, the property Age in your Person class should be decorated with a RangeValidator, just as follows:
[RangeValidator(21, RangeBoundaryType.Inclusive, 100, RangeBoundaryType.Inclusive, Ruleset = "GenericPerson", MessageTemplate = "Invalid value")]
public int Age
{ … }
Back to your ASP.NET application, we assume you already have a Web Form to collect Person data. The markup corresponding to the control used to collect the age value might look as follows:
<asp:TextBox ID="AgeTextBox" runat="server"></asp:TextBox>
The first thing you need to do in order to take advantage of VAP and your already defined validation rules is adding references to Microsoft.Practices.EnterpriseLibrary.Validation and Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet –which allows connecting VAP with ASP.NET applications through the usage of the PropertyProxyValidator class. You may also need to add a reference to the assembly where the business entities are defined, along with their validation rules. More details on how to prepare your ASP.NET application in order to use the Validation Application Block can be found on the block’s documentation.
Once your application has the needed references you’ll need to register the namespace in which the VAP classes are defined. This can be accomplished by inserting the following code after the Page directive in your ASPX markup:
<%@ Register Assembly="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet"
Namespace="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet"
TagPrefix="vab" %>
Now you can add validators to your controls, associating them to the existing controls in your web form. The resulting markup should look like follows:
<asp:TextBox ID="AgeTextBox" runat="server" CausesValidation="True"></asp:TextBox>
<vab:propertyproxyvalidator id="AgeValidator" runat="server" ControlToValidate="AgeTextBox" PropertyName="Age" RulesetName="GenericPerson" SourceTypeName="BusinessLayer.Person">Please enter a number between 21 and 100</vab:propertyproxyvalidator>
That’s it! Your form has got validation through the usage of the VAB. You can also use the validation defined by the referenced business entities in other web forms or in a desktop application. Now if an invalid value is submitted an error message will be displayed:
![clip_image002[4] clip_image002[4]](http://www.aggiorno.com/blogs/aggiornings/image.axd?picture=WindowsLiveWriter/Addingadvancedvalidationt.NETapplication_10CDD/clip_image002%5B4%5D_thumb.gif)
Some external references
- The Validation Application Block provides several other validators besides the RangeValidator used in this post. For more information about other validators please refer to this topic on the online documentation.
- You can add customized validators for more complex validation rules. Please refer to this topic for more details on how to extend existing validators and how to develop your new ones.
[advertisement] Try
Aggiorno for free, the assistant tool for web development you always wanted.