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.