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()); });
Posted on 28 February, 2012, in ASP.NET, Dev Stuff, MVC, Uncategorized and tagged ASP.Net, ASP.NET Web API, MVC 4, validation errors. Bookmark the permalink. 3 Comments.
Very nice. Noted for future use. Thanks for taking the time to write this up! 🙂
Nice. I cannot wait to give MVC 4 a crack!
Thanks for this.