Using the Enterprise Library 3 Validation Block in ASP.NET
page 3 of 4
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 23070/ 45

ASP.NET Validation

So how does this validation work with an ASP.NET page?  In the Quick Starts folder that is installed with Enterprise Library 3, an ASP.NET integration component residing there contains a PropertyProxyValidator.  The validator requires the name of the business object type to validate the name of the property that contains the specific validation details and the Ruleset to enforce.  Using this, it can apply the validators against the data entered and specify a detailed error message.  In the example below a form has been setup to use the business object validators, rather than the ASP.NET default validators, to validate against a the business object defined above.

Listing 2

<table border="0">
<tr>
      <td>Name</td>
      <td>
            <asp:TextBox ID="txtName" runat="server" />
            <el:PropertyProxyValidator ID="ppvName" 
             runat="server" 
             SourceTypeName="Mains.Examples.User,App_Code"
             PropertyName="Name" RulesetName="primary"
           ControlToValidate="txtName">
          *</el:PropertyProxyValidator>
      </td>
</tr>
<tr>
      <td>Phone</td>
      <td>
            <asp:TextBox ID="txtPhone" runat="server" />
            <el:PropertyProxyValidator ID="ppvPhone" 
             runat="server" 
             SourceTypeName="Mains.Examples.User,App_Code"
             PropertyName="Phone" RulesetName="primary" 
           ControlToValidate="txtPhone">
          *</el:PropertyProxyValidator>
      </td>
</tr>
<tr>
      <td>Email</td>
      <td>
            <asp:TextBox ID="txtEmail" runat="server" />
            <el:PropertyProxyValidator ID="ppvEmail"
           runat="server" 
           SourceTypeName="Mains.Examples.User,App_Code"
             PropertyName="Email" RulesetName="primary"  
           ControlToValidate="txtEmail">
          *</el:PropertyProxyValidator>
      </td>
</tr>
<tr>
      <td colspan="2">
            <asp:LinkButton ID="lnkSave" runat="server" 
           OnClick="lnkSave_Click">Save</asp:LinkButton>
      </td>
</tr>
</table>

In the code-behind page I ensure that the page is valid before creating the user in the form.  This could then refresh a GridView control or some other mechanism that displays the users.  The following result to save the user is shown below.

Listing 3

public void lnkSave_Click(object sender, EventArgs e)
{
  if (Page.IsValid)
  {
    User user = new User();
    user.Name = this.txtName.Text;
    user.Phone = this.txtPhone.Text;
    user.Email = this.txtEmail.Text;
    //You could add this user to a repository
    UsersRepository.Add(user);
    Response.Write("<strong>User added</strong>");
  }
}

Upon the page not being valid, the error warnings are shown in the screen.  They are shown below:

·         The name must have at least a space between the names.
The name must be between 3 and 150 characters long.

·         The phone number must be between 7 and 10 characters long.
The phone number is not a valid phone number; it can be numbers only.

·         The email address must be between 7 and 150 characters long.
The email must have at least an @ and at least one decimal.


View Entire Article

User Comments

Title: Validation   
Name: Manuel
Date: 2008-08-18 6:35:09 AM
Comment:
Sory, just to add, i've built those rules in app.config (to be able to edit those validations rules easealy)

Manuel
Title: Validation   
Name: Manuel
Date: 2008-08-18 6:33:15 AM
Comment:
Maybe i've explained myself wrong...i want to be able to use RuleSet's to validate objects according to those rules. I've seen this article (http://www.devx.com/dotnet/Article/34950/1954) that seems to be able to do what i want, but i built a small demo projecto and i'm not beeing able to validate against those rules. Have you ever used it? Can you help me with this small demo?

Thank's,
Manuel
Title: Validation Reply   
Name: Brian
Date: 2008-08-08 9:02:43 AM
Comment:
I don't believe they have this built in, but it should be possible to achieve. There is a PropertyComparisonValidator which I believe compares the values of two properties together, but if you use a tool like Reflector, you may be able to look at the code and implement something very similar to suite your needs.
Title: Validation   
Name: Manuel
Date: 2008-08-08 5:36:44 AM
Comment:
Let's say you have an extra property in your User class (userGender) and if the object is set with this attribute to "Female" it should validate the phone number, otherwise it should't. Is it possible to define this?

Best regards,
Manuel
Title: web.config   
Name: Brian
Date: 2007-09-25 8:34:51 PM
Comment:
Check the web.config; this is the preferred area to do so over the @Register declarations. It is in the ASPNET.Integration companion DLL, which you can see in the .NET references list (I believe).
Title: How is the control available on page?   
Name: Tim
Date: 2007-09-25 4:38:06 PM
Comment:
Uhhhh, how did you get the "el:PropertyProxyValidator" control to work on the page. Obviously, it must have been imported through an @Register, or a line in web.config, but that would be an extremely handy thing to show in your article. Thanks.
Title: Client Side Validation Reply   
Name: Brian Mains
Date: 2007-04-18 11:32:23 AM
Comment:
You are right, there are trade-off's with this approach; if you plan to do business validation, it would make sense to do it with the EL Validation block, especially if most of it is done on the server anyway (complex validation logic that is).

Sometimes I prefer it that way because of some of the problems I've experienced with client-side validation in certain situations, but never the less, it is very helpful to have that client validation capability.

Everything is a trade-off, and it depends on popularity of the applications, network traffic amount, speed, etc.
Title: Client Side Validation   
Name: Eric
Date: 2007-04-10 9:05:14 AM
Comment:
I can see the benefit of having the validation centralized in the business objects. This way you do not have to duplicate the validation code on the presentation page (WebForm/WinForm). But by doing this, we loose the client side validation that asp validators provide. This means that all validation needs a round trip to the server. So I guess there is a trade off, or should you still use simple validation for example like the requiredfiledvalidator that asp.net provides to limit round trips to the server?
Title: Author Reply   
Name: Brian Mains
Date: 2007-04-06 11:39:06 AM
Comment:
Hello Joydip,

That is soon to be released, and I am working on one that utilizes Localization, and shows how that can be implemented in your validators.

Brian
Title: Nice   
Name: Joydip
Date: 2007-04-06 10:40:16 AM
Comment:
Hi Brian,

This is a nice article. I liked it. But, what about building the custom validators?

-Joydip

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 6:54:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search