Created
September 10, 2013 06:27
-
-
Save Graceas/6505670 to your computer and use it in GitHub Desktop.
Revisions
-
Graceas created this gist
Sep 10, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ function form_callback(target) { target = $(target); $.ajax({ url: target.attr('action'), type: target.attr('method'), data: target.serialize(), dataType: 'json', beforeSend: function() { //clear errors target.find('.alert-error').remove(); //disable buttons target.find('[type="submit"]').attr('disabled', 'disabled'); }, success: function(response) { target.find('[type="submit"]').removeAttr('disabled'); if (response.status == 'error') { //process errors process_errors(target, response.errors); } else { if (response.status == 'redirect') { window.location.href = response.redirect; } else { //stay down if (response.status == 'data') { target.html($(response.data)); } } } } }); return false; } function process_errors(target, errors) { if (target.find('.global-errors-container').length > 0) { for (var i in errors.global) { target.find('.global-errors-container').append( '<div class="alert-error">' + errors.global[i] + '</div>' ); } } for (var field_id in errors.fields) { if ($('#' + field_id).length > 0) { $('#' + field_id).before( '<div class="alert-error">' + errors.fields[field_id] + '</div>' ); } } }