diff --git a/public/assets/js/common.js b/public/assets/js/common.js new file mode 100644 index 00000000..9dea7705 --- /dev/null +++ b/public/assets/js/common.js @@ -0,0 +1,14 @@ +$(function() { + $(document).ajaxError(function(event, response, ajaxSettings, thrownError) { + switch (response.status) { + case 500: + default: + swal({ + title: 'Whoops!', + text: 'We encountered a problem. Please contact technical support.', + type: 'error' + }); + break; + } + }); +}); \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 410b42fc..84dbcbb4 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -694,6 +694,7 @@ + diff --git a/templates/battery-manufacturer/form.html.twig b/templates/battery-manufacturer/form.html.twig index 0cbcedd1..1e6cc90d 100644 --- a/templates/battery-manufacturer/form.html.twig +++ b/templates/battery-manufacturer/form.html.twig @@ -86,38 +86,40 @@ } }); }).fail(function(response) { - var errors = response.responseJSON.errors; - var firstfield = false; + if (response.status == 422) { + var errors = response.responseJSON.errors; + var firstfield = false; - // remove all error classes first - removeErrors(); + // remove all error classes first + removeErrors(); - // display errors contextually - $.each(errors, function(field, msg) { - var formfield = $("[name='" + field + "']"); - var label = $("label[data-field='" + field + "']"); - var msgbox = $(".form-control-feedback[data-field='" + field + "']"); + // display errors contextually + $.each(errors, function(field, msg) { + var formfield = $("[name='" + field + "']"); + var label = $("label[data-field='" + field + "']"); + var msgbox = $(".form-control-feedback[data-field='" + field + "']"); - // add error classes to bad fields - formfield.addClass('form-control-danger'); - label.addClass('has-danger'); - msgbox.html(msg).addClass('has-danger').removeClass('hide'); + // add error classes to bad fields + formfield.addClass('form-control-danger'); + label.addClass('has-danger'); + msgbox.html(msg).addClass('has-danger').removeClass('hide'); - // check if this field comes first in DOM - var domfield = formfield.get(0); + // check if this field comes first in DOM + var domfield = formfield.get(0); - if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) { - firstfield = domfield; - } - }); + if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) { + firstfield = domfield; + } + }); - // focus on first bad field - firstfield.focus(); + // focus on first bad field + firstfield.focus(); - // scroll to above that field to make it visible - $('html, body').animate({ - scrollTop: $(firstfield).offset().top - 200 - }, 100); + // scroll to above that field to make it visible + $('html, body').animate({ + scrollTop: $(firstfield).offset().top - 200 + }, 100); + } }); }); diff --git a/templates/battery-manufacturer/list.html.twig b/templates/battery-manufacturer/list.html.twig index 8c13c188..7d345225 100644 --- a/templates/battery-manufacturer/list.html.twig +++ b/templates/battery-manufacturer/list.html.twig @@ -137,12 +137,6 @@ }).done(function(response) { table.row(btn.parents('tr')).remove(); table.reload(); - }).fail(function() { - swal({ - title: 'Whoops', - text: 'An error occurred while deleting this item. Please contact support.', - type: 'error' - }); }); } }); diff --git a/templates/battery-model/form.html.twig b/templates/battery-model/form.html.twig index dd14199f..9bdf618f 100644 --- a/templates/battery-model/form.html.twig +++ b/templates/battery-model/form.html.twig @@ -86,38 +86,40 @@ } }); }).fail(function(response) { - var errors = response.responseJSON.errors; - var firstfield = false; + if (response.status == 422) { + var errors = response.responseJSON.errors; + var firstfield = false; - // remove all error classes first - removeErrors(); + // remove all error classes first + removeErrors(); - // display errors contextually - $.each(errors, function(field, msg) { - var formfield = $("[name='" + field + "']"); - var label = $("label[data-field='" + field + "']"); - var msgbox = $(".form-control-feedback[data-field='" + field + "']"); + // display errors contextually + $.each(errors, function(field, msg) { + var formfield = $("[name='" + field + "']"); + var label = $("label[data-field='" + field + "']"); + var msgbox = $(".form-control-feedback[data-field='" + field + "']"); - // add error classes to bad fields - formfield.addClass('form-control-danger'); - label.addClass('has-danger'); - msgbox.html(msg).addClass('has-danger').removeClass('hide'); + // add error classes to bad fields + formfield.addClass('form-control-danger'); + label.addClass('has-danger'); + msgbox.html(msg).addClass('has-danger').removeClass('hide'); - // check if this field comes first in DOM - var domfield = formfield.get(0); + // check if this field comes first in DOM + var domfield = formfield.get(0); - if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) { - firstfield = domfield; - } - }); + if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) { + firstfield = domfield; + } + }); - // focus on first bad field - firstfield.focus(); + // focus on first bad field + firstfield.focus(); - // scroll to above that field to make it visible - $('html, body').animate({ - scrollTop: $(firstfield).offset().top - 200 - }, 100); + // scroll to above that field to make it visible + $('html, body').animate({ + scrollTop: $(firstfield).offset().top - 200 + }, 100); + } }); }); diff --git a/templates/battery-model/list.html.twig b/templates/battery-model/list.html.twig index bec1aa6d..4af0226d 100644 --- a/templates/battery-model/list.html.twig +++ b/templates/battery-model/list.html.twig @@ -137,12 +137,6 @@ }).done(function(response) { table.row(btn.parents('tr')).remove(); table.reload(); - }).fail(function() { - swal({ - title: 'Whoops', - text: 'An error occurred while deleting this item. Please contact support.', - type: 'error' - }); }); } }); diff --git a/templates/battery-size/form.html.twig b/templates/battery-size/form.html.twig index 1f309d40..4736a772 100644 --- a/templates/battery-size/form.html.twig +++ b/templates/battery-size/form.html.twig @@ -86,38 +86,40 @@ } }); }).fail(function(response) { - var errors = response.responseJSON.errors; - var firstfield = false; + if (response.status == 422) { + var errors = response.responseJSON.errors; + var firstfield = false; - // remove all error classes first - removeErrors(); + // remove all error classes first + removeErrors(); - // display errors contextually - $.each(errors, function(field, msg) { - var formfield = $("[name='" + field + "']"); - var label = $("label[data-field='" + field + "']"); - var msgbox = $(".form-control-feedback[data-field='" + field + "']"); + // display errors contextually + $.each(errors, function(field, msg) { + var formfield = $("[name='" + field + "']"); + var label = $("label[data-field='" + field + "']"); + var msgbox = $(".form-control-feedback[data-field='" + field + "']"); - // add error classes to bad fields - formfield.addClass('form-control-danger'); - label.addClass('has-danger'); - msgbox.html(msg).addClass('has-danger').removeClass('hide'); + // add error classes to bad fields + formfield.addClass('form-control-danger'); + label.addClass('has-danger'); + msgbox.html(msg).addClass('has-danger').removeClass('hide'); - // check if this field comes first in DOM - var domfield = formfield.get(0); + // check if this field comes first in DOM + var domfield = formfield.get(0); - if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) { - firstfield = domfield; - } - }); + if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) { + firstfield = domfield; + } + }); - // focus on first bad field - firstfield.focus(); + // focus on first bad field + firstfield.focus(); - // scroll to above that field to make it visible - $('html, body').animate({ - scrollTop: $(firstfield).offset().top - 200 - }, 100); + // scroll to above that field to make it visible + $('html, body').animate({ + scrollTop: $(firstfield).offset().top - 200 + }, 100); + } }); }); diff --git a/templates/battery-size/list.html.twig b/templates/battery-size/list.html.twig index 7afe2389..ecde061b 100644 --- a/templates/battery-size/list.html.twig +++ b/templates/battery-size/list.html.twig @@ -137,12 +137,6 @@ }).done(function(response) { table.row(btn.parents('tr')).remove(); table.reload(); - }).fail(function() { - swal({ - title: 'Whoops', - text: 'An error occurred while deleting this item. Please contact support.', - type: 'error' - }); }); } }); diff --git a/templates/battery/form.html.twig b/templates/battery/form.html.twig index 51c8cdb8..f7dfe2ca 100644 --- a/templates/battery/form.html.twig +++ b/templates/battery/form.html.twig @@ -32,183 +32,178 @@ -