Home
John Rummell edited this page Jan 11, 2015
·
2 revisions
Install the xVal.WebForms package with NuGet.
Or you can use the package manager console:
PM> Install-Package xVal.WebForms
Decorate a model's properties with validation attributes. You can also add class validation attributes or implement IValidatableObject.
public class Booking : IValidatableObject
{
[Required(ErrorMessage = "Client name is required.")]
[StringLength(15, ErrorMessage = "Client name must be less than 15 characters.")]
public string ClientName { get; set; }
[Range(1, 20, ErrorMessage = "Number of guests must be between 1 and 20.")]
public int NumberOfGuests { get; set; }
[Required(ErrorMessage = "Arrival date is required.")]
[DataType(DataType.Date, ErrorMessage = "Arrival date is invalid.")]
public DateTime ArrivalDate { get; set; }
[Required(ErrorMessage = "Smoking type is requried.")]
public SmokingType SmokingType { get; set; }
[Required(ErrorMessage = "Email address is required.")]
[DataType(DataType.EmailAddress, ErrorMessage = "Email address is invalid.")]
public string EmailAddress { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (ArrivalDate == DateTime.MinValue)
{
yield return new ValidationResult("Arrival date is required.", new[] {"ArrivalDate"});
}
// Business rule: Can't place bookings on Sundays
if (ArrivalDate.DayOfWeek == DayOfWeek.Sunday)
{
yield return new ValidationResult("Bookings are not permitted on Sundays.", new[] {"ArrivalDate"});
}
}
}
The NuGet package installs the jquery and jquery validate scripts in the Scripts folder.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<script src="<%: ResolveUrl("~/Scripts/jquery-1.4.4.min.js") %>" type="text/javascript"></script>
<script src="<%: ResolveUrl("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
Create a form and add a ModelPropertyValidator for each input control.
<fieldset class="booking">
<legend>Booking</legend>
<asp:ValidationSummary ID="valSummary" runat="server" />
<label for="txtClientName">
Your name:</label>
<asp:TextBox ID="txtClientName" runat="server" />
<val:ModelPropertyValidator ID="valClientName" runat="server" CssClass="validator"
ControlToValidate="txtClientName" Display="Dynamic" PropertyName="ClientName"
ModelType="xVal.WebForms.Demo.Booking" />
<label for="txtNumberOfGuests">
Number of guests:</label>
<asp:TextBox ID="txtNumberOfGuests" runat="server" />
<val:ModelPropertyValidator ID="valNumberOfGuests" runat="server" CssClass="validator"
ControlToValidate="txtNumberOfGuests" Display="Dynamic" PropertyName="NumberOfGuests"
ModelType="xVal.WebForms.Demo.Booking" />
<label for="txtArrivalDate">
Arrival date:</label>
<asp:TextBox ID="txtArrivalDate" runat="server" />
<val:ModelPropertyValidator ID="valArrivalDate" runat="server" CssClass="validator"
ControlToValidate="txtArrivalDate" Display="Dynamic" PropertyName="ArrivalDate"
ModelType="xVal.WebForms.Demo.Booking" />
<label for="txtEmailAddress">
Email address:</label>
<asp:TextBox ID="txtEmailAddress" runat="server" />
<val:ModelPropertyValidator ID="valEmailAddress" runat="server" CssClass="validator"
ControlToValidate="txtEmailAddress" Display="Dynamic" PropertyName="EmailAddress"
ModelType="xVal.WebForms.Demo.Booking" />
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></div>
</fieldset>
Validation groups require that your pages inherit from ModelPage. Long story short, the validators implement IValidator instead of BaseValidator so that the standard validation scripts aren't injected into the page.
Download the source and take a look at the demo website for a full example.
- Create Work Items for any bugs or feature request before committing changes.
- Make sure all unit tests pass before committing a change. This means all existing tests and new tests specific to the change.
- Make sure to include the Work Item number when committing changes.
- If applicable, include a sample page in the demo project that demonstrates the new or changed behavior.