Blog Archives
Using Web API Validation with jQuery Validate
Building on my last post about validating your model with Web API, if you’re calling a Web API controller from JavaScript you may need to parse the validation result and display it on the screen.
Most people using MVC would be using the jQuery Validate plugin that’s been included with the default template for quite a while now. While most validations are performed using JavaScript adapters, some are only performed server side. As a result, the regular unobtrusive JavaScript adapters will not catch this before the post occurs. This means that you if you are using JavaScript requests with Web API to handle data manipulation you will need to somehow manually handle the validation errors that will be returned.
Plugging into jQuery Validation is actually quite easy… To validate a form, simply select the form using jQuery and call .validate() on it – e.g.
var validator = $('.main-content form').validate();
This will return a validator object will a few handy methods on it. Two of which are valid() and showErrors(). The valid method will return a Boolean value indicating whether the form is valid or not and the showErrors method will show any validation errors on the current form. The showErrors method also accepts an object that defines any additional error messages you wish to display – e.g. to display the message “The title is incorrect” for a property named Title:
validator.showErrors({ Title: 'The title is incorrect.' });
Now, assuming I a view with the following mark-up inside the form, I should see a validation error:
<div class="editor-label">@Html.LabelFor(model => model.Title)</div> <div class="editor-field"> @Html.TextBoxFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div>
But how do we connect this to Web API…? Well, if you’ve read my previous post you’ll recall that calling a Web API controller’s PUT action that’s decorated with the ValidateFilter attribute I created will return a collection of validation errors if the model is not valid. To test this, I’ll modify my TodoApiController from the previous post as follows:
[ValidateFilter] public void Put(int id, TodoItem value) { if (value.Title == "hi there") ModelState.AddModelError("Title", "The title is incorrect."); if (!ModelState.IsValid) return; db.Entry(value).State = EntityState.Modified; db.SaveChanges(); }
I should now receive a validation error whenever I try to update an item with the title “hi there”. Let’s write some jQuery to submit my form:
function updateItem(form, url) { var validator = form.validate(), serialized = form.serializeArray() data = { }; if (!validator.valid()) { return; } // turn the array of form properties into a regular JavaScript object for (var i = 0; i < serialized.length; i++) { data[serialized[i].name] = serialized[i].value; } $.ajax({ type: 'PUT', // Update Action url: url, // API Url e.g. http://localhost:9999/api/TodoApi/1 data: data, // e.g. { TodoItemId: 1, Title: 'hi there', IsDone: false } dataType: 'JSON', success: function () { alert('success'); }, error: function (jqXhr) { extractErrors(jqXhr, validator); } }); }
Now let’s look at extractErrors:
function extractErrors(jqXhr, validator) { var data = JSON.parse(jqXhr.responseText), // parse the response into a JavaScript object errors = { }; for (var i = 0; i < data.length; i++) { // add each error to the errors object errors[data[i].key] = data[i].value; } validator.showErrors(errors); // show the errors using the validator object }
Lastly, attaching to the form’s submit event will call this whenever the Enter key is hit or the Submit button is clicked:
$('.main-content form').submit(function () { updateItem($(this), '/api/TodoApi/' + $('#TodoItemId').val()); });
Validating your model with Web API
One of the great things about ASP.NET 4.5’s Web API is that it’s built on the same (or similar) principles as MVC. This means that you get a lot of cool things out of your API controllers from MVC – like Action Filters.
While building my first Web API controller, I wanted to ensure that a creation or an update of an item was only done if that item was valid. I also wanted to pass any validation errors back to the client. This looks quite difficult at first because the Put and Post functions on an ApiController can’t return a result. Action Filters to the rescue!
With a simple action filter attribute, you can ensure that your models are validated and the errors are returned in a simple format to the client by decorating the appropriate methods.
Note: Code also available on Gist – https://gist.github.com/1920999
public class ValidateFilterAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { var modelState = actionExecutedContext.ActionContext.ModelState; if (!modelState.IsValid) { var errors = modelState .Where(s => s.Value.Errors.Count > 0) .Select(s => new KeyValuePair<string, string>(s.Key, s.Value.Errors.First().ErrorMessage)) .ToArray(); actionExecutedContext.Result = new HttpResponseMessage<KeyValuePair<string, string>[]>( errors, HttpStatusCode.BadRequest ); } base.OnActionExecuted(actionExecutedContext); }
Now, for the controller implementation…
public class TodoApiController : ApiController { private BetterMobileSpaContext db = new BetterMobileSpaContext(); // GET /api/todoapi public IEnumerable<TodoItem> Get() { return db.TodoItems.ToList(); } // GET /api/todoapi/5 public TodoItem Get(int id) { return db.TodoItems.Find(id); } // POST /api/todoapi [ValidateFilter] public void Post(TodoItem value) { if (!ModelState.IsValid) return; db.TodoItems.Add(value); db.SaveChanges(); } // PUT /api/todoapi/5 [ValidateFilter] public void Put(int id, TodoItem value) { if (!ModelState.IsValid) return; db.Entry(value).State = EntityState.Modified; db.SaveChanges(); } // DELETE /api/todoapi/5 public void Delete(int id) { TodoItem todoitem = db.TodoItems.Find(id); db.TodoItems.Remove(todoitem); db.SaveChanges(); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } }
Now a Create (PUT) and an Update (POST) request will validate the model before invoking the appropriate action. Calling Create from using a JSON request with an invalid model would look something like this:
Request
Method: PUT
Body: { Title: ”, IsDone: false }
Response
Status Code: 400 (Bad Request)
Body: [{ “key”: “Title”, “value”: “The Title field is required” }]
Using a valid model will simply result in a 200 (OK) response with no body.
NOTE: while writing this post I stumbled on these two blog posts that did the same thing and adapted my code:
- http://weblogs.asp.net/cibrax/archive/2012/02/23/validating-your-models-in-asp-net-web-api.aspx
- http://blog.alexonasp.net/post/2012/02/16/ASPNET-MVC-4-public-beta-including-ASPNET-Web-API.aspx
EDIT 2012-02-27: Updated code to replace ValidationError type with KeyValuePair and changed filter to be after action execution so controller can perform any extra validation.