I've got a form that is using unobtrusive JQuery validation along with a validation summary. It works great. However, this form does an Ajax POST which returns a JSON result. If the result == true, then I continue. However, if the JSON result returns an array of messages, I want to fire the form validation in order to update the fields. Errors are returned as follows:
{
"errors": [
{ "key" : "NameFirst", "message": "This is the message" },
{ "key" : "NameLast", "message": "This is the message" }
]
}
I parse the JSON result and call showErrors() like so:
for (var j = 0; j < response.errors.length; j++) {
var key = response.errors[j].key;
var error = {};
error[key] = response.errors[j].message;
$form.data('validator').showErrors(error);
}
This correctly highlights the fields, but it doesn't update the validation summary. How can I get that to update?
Also, sometimes the errors are generic and don't map to a specific property/field in the model. In that case, they are returned with null keys like:
{
"errors": [
{ "key" : null, "message": "This is the message" },
{ "key" : null, "message": "This is the other message" },
{ "key" : "NameFirst", "message": "This is the message" },
{ "key" : "NameLast", "message": "This is the message" }
]
}
I can't call showErrors on those because they don't map to a field identifier. Once I'm told how to update the summary, I sure I can append items to the list for the generic messages, but I'm open to other suggestions. Thanks!
UPDATE
Here is what I ended up doing that seems to work quite well. I have to build the summary manually in addition to calling showErrors on the valid keyed errors:
var $summary = $form.find("[data-valmsg-summary=true]")
.addClass("validation-summary-errors")
.removeClass("validation-summary-valid");
var $ul = $summary.find("ul").empty();
var error = {};
$.each(response.errors, function () {
if (this.key)
error[this.key] = this.message;
$("<li />").html(this.message).appendTo($ul);
});
$form.validate().showErrors(error);
I hope this helps others.