29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

One of the nice features about the MVC client-side validation is that the same attributes used to specify validation rules are<br />

applied at the client and at the server. This means that data from browsers that do not support JavaScript are subject to the same<br />

validation as those that do, without requiring any additional effort.<br />

MVC CLIENT VALIDATION VERSUS JQUERY VALIDATION<br />

The MVC client-validation features are built on top of the jQuery Validation library. If you prefer, you can use the Validation<br />

library directly and ignore the MVC features. The Validation library is flexible and feature-rich. It is well worth exploring, if<br />

only to understand how to customize the MVC features to take best advantage of the available validation options. I cover the<br />

jQuery Validation library in depth in my Pro jQuery 2.0 book, also published by Apress.<br />

Performing Remote Validation<br />

The last validation feature I will look at in this chapter is remote validation. This is a client-side validation technique that invokes an<br />

action method on the server to perform validation.<br />

A common example of remote validation is to check whether a username is available in applications when such names must be<br />

unique; the user submits the data, and the client-side validation is performed. As part of this process, an Ajax request is made to<br />

the server to validate the username that has been requested. If the username has been taken, a validation error is displayed so that<br />

the user can enter another value.<br />

This may seem like regular server-side validation, but there are some benefits to this approach. First, only some properties will<br />

be remotely validated; the client-side validation benefits still apply to all the other data values that the user has entered. Second, the<br />

request is relatively lightweight and is focused on validation, rather than processing an entire model object.<br />

The third difference is that the remote validation is performed in the background. The user doesn’t have to click the submit<br />

button and then wait for a new view to be rendered and returned. It makes for a more responsive user experience, especially when<br />

there is a slow network between the browser and the server.<br />

That said, remote validation is a compromise. It strikes a balance between client-side and server-side validation, but it does<br />

require requests to the application server, and it is not as quick to validate as normal client-side validation.<br />

The first step toward using remote validation is to create an action method that can validate one of the model properties. I am<br />

going to validate the Date property of the Appointment model to ensure that the requested appointment is in the future.<br />

(This is one of the original validation rules I used at the start of the chapter, but which isn’t possible to validate using the standard<br />

client-side validation features.) In Listing 25-24, you can see the ValidateDate action method that I added to the Home<br />

controller.<br />

Listing 25-24. Adding a Validation Action Method to the HomeController.cs File<br />

using System;<br />

using System.Web.Mvc;<br />

using ModelValidation.Models;<br />

namespace ModelValidation.Controllers {<br />

public class HomeController : Controller {<br />

public ViewResult MakeBooking() {<br />

return View(new Appointment { Date = DateTime.Now });<br />

}<br />

[HttpPost]<br />

public ViewResult MakeBooking(Appointment appt) {<br />

if (ModelState.IsValid) {<br />

// statements to store new Appointment in a<br />

// repository would go here in a real project<br />

return View("Completed", appt);<br />

} else {<br />

return View();<br />

}<br />

687

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!