Display per-line errors

This commit is contained in:
Oliver 2021-07-12 19:42:06 +10:00
parent 51314a0261
commit 5329e3e56c
2 changed files with 19 additions and 22 deletions

View File

@ -816,33 +816,27 @@ function handleFormErrors(errors, fields, options) {
}
for (field_name in errors) {
if (field_name in fields) {
// Add the 'has-error' class
$(options.modal).find(`#div_id_${field_name}`).addClass('has-error');
// Add the 'has-error' class
$(options.modal).find(`#div_id_${field_name}`).addClass('has-error');
var field_dom = $(options.modal).find(`#errors-${field_name}`); // $(options.modal).find(`#id_${field_name}`);
var field_dom = $(options.modal).find(`#errors-${field_name}`); // $(options.modal).find(`#id_${field_name}`);
var field_errors = errors[field_name];
var field_errors = errors[field_name];
// Add an entry for each returned error message
for (var idx = field_errors.length-1; idx >= 0; idx--) {
// Add an entry for each returned error message
for (var idx = field_errors.length-1; idx >= 0; idx--) {
var error_text = field_errors[idx];
var error_text = field_errors[idx];
var html = `
<span id='error_${idx+1}_id_${field_name}' class='help-block form-error-message'>
<strong>${error_text}</strong>
</span>`;
var html = `
<span id='error_${idx+1}_id_${field_name}' class='help-block form-error-message'>
<strong>${error_text}</strong>
</span>`;
field_dom.append(html);
}
} else {
console.log(`WARNING: handleFormErrors found no match for field '${field_name}'`);
field_dom.append(html);
}
}
}

View File

@ -167,7 +167,12 @@ function adjustStock(items, options={}) {
<td id='part_${pk}'><img src='${image}' class='hover-img-thumb'> ${item.part_detail.full_name}</td>
<td id='stock_${pk}'>${quantity}${status}</td>
<td id='location_${pk}'>${location}</td>
<td id='action_${pk}'>${actionInput}</td>
<td id='action_${pk}'>
<div id='div_id_${pk}'>
${actionInput}
<div id='errors-${pk}'></div>
</div>
</td>
<td id='buttons_${pk}'>${buttons}</td>
</tr>`;
@ -259,9 +264,7 @@ function adjustStock(items, options={}) {
error: function(xhr) {
switch (xhr.status) {
case 400:
console.log('400 bad request');
console.log(xhr.responseJSON);
// Handle errors for standard fields
handleFormErrors(
xhr.responseJSON,