Skip to content

Instantly share code, notes, and snippets.

@nowk
Created March 18, 2012 22:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nowk/2083155 to your computer and use it in GitHub Desktop.
Save nowk/2083155 to your computer and use it in GitHub Desktop.
Formtastic errors w/ ajax + knockout.js and responds_to :json
$.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>');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment