Changeset View
Changeset View
Standalone View
Standalone View
src/resources/js/app.js
Show All 29 Lines | error => { | ||||
error_msg = "Form validation error" | error_msg = "Form validation error" | ||||
$.each(error.response.data.errors || {}, (idx, msg) => { | $.each(error.response.data.errors || {}, (idx, msg) => { | ||||
$('form').each((i, form) => { | $('form').each((i, form) => { | ||||
const input_name = ($(form).data('validation-prefix') || '') + idx | const input_name = ($(form).data('validation-prefix') || '') + idx | ||||
const input = $('#' + input_name) | const input = $('#' + input_name) | ||||
if (input.length) { | if (input.length) { | ||||
// Create an error message\ | |||||
// API responses can use a string, array or object | |||||
let msg_text = '' | |||||
if ($.type(msg) !== 'string') { | |||||
$.each(msg, (index, str) => { | |||||
msg_text += str + ' ' | |||||
}) | |||||
} | |||||
else { | |||||
msg_text = msg | |||||
} | |||||
let feedback = $('<div class="invalid-feedback">').text(msg_text) | |||||
if (input.is('.listinput')) { | |||||
// List input widget | |||||
let list = input.next('.listinput-widget') | |||||
list.children(':not(:first-child)').each((index, element) => { | |||||
if (msg[index]) { | |||||
$(element).find('input').addClass('is-invalid') | |||||
} | |||||
}) | |||||
list.addClass('is-invalid').next('.invalid-feedback').remove() | |||||
list.after(feedback) | |||||
} | |||||
else { | |||||
// Standard form element | |||||
input.addClass('is-invalid') | input.addClass('is-invalid') | ||||
input.parent().find('.invalid-feedback').remove() | input.parent().find('.invalid-feedback').remove() | ||||
input.parent().append($('<div class="invalid-feedback">') | input.parent().append(feedback) | ||||
.text($.type(msg) === 'string' ? msg : msg.join(' '))) | } | ||||
return false | return false | ||||
} | } | ||||
}); | }); | ||||
}) | }) | ||||
$('form .is-invalid').first().focus() | $('form .is-invalid:not(.listinput-widget)').first().focus() | ||||
} | } | ||||
else if (error.response && error.response.data) { | else if (error.response && error.response.data) { | ||||
error_msg = error.response.data.message | error_msg = error.response.data.message | ||||
} | } | ||||
else { | else { | ||||
error_msg = error.request ? error.request.statusText : error.message | error_msg = error.request ? error.request.statusText : error.message | ||||
} | } | ||||
▲ Show 20 Lines • Show All 94 Lines • Show Last 20 Lines |