public
Last active

Formtastic errors w/ ajax + knockout.js and responds_to :json

  • Download Gist
application.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$.ajax({
error: function(jqXHR, textStatus, errorThrown) {
// reset
$('.formtastic li').removeClass("error");
$('.formtastic li .inline-errors').remove();
 
// set the errors
// TODO turn into dynamic
// TODO include errors for :base and #semantic_errors
var errors = ko.mapping.toJS(ko.mapping.fromJSON(jqXHR.responseText));
$.each(errors, function(key, value) {
$(".formtastic.menu li#menu_"+key+"_input.required").addClass("error");
$(".formtastic.menu li#menu_"+key+"_input.required").append('<p class="inline-errors">'+value.join(", ")+'</p>');
});
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.