// HTML form part // < form class="form" action="{{ path('...') }}" method="POST" onsubmit="form_callback(this); return false;" > 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( '
' + errors.global[i] + '
' ); } } for (var field_id in errors.fields) { if ($('#' + field_id).length > 0) { $('#' + field_id).before( '
' + errors.fields[field_id] + '
' ); } } }