From 118b0d4bdda991016c26a30d6bb11d481afa6b58 Mon Sep 17 00:00:00 2001 From: Corban-Lee Date: Tue, 8 Oct 2024 12:31:49 +0100 Subject: [PATCH] form validation text --- apps/home/static/home/js/tables.js | 28 +++++++++++++++++++- apps/home/templates/home/modals/editSub.html | 4 +-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/apps/home/static/home/js/tables.js b/apps/home/static/home/js/tables.js index fa8e16a..cd67723 100644 --- a/apps/home/static/home/js/tables.js +++ b/apps/home/static/home/js/tables.js @@ -382,7 +382,13 @@ function setDefaultModalData($modal) { }); } +function clearValidation($modal) { + $modal.find(".invalid-feedback").remove(); + $modal.find(".is-invalid").removeClass("is-invalid"); +} + async function loadModalData($modal, url) { + clearValidation($modal); const data = await ajaxRequest(url, "GET"); $modal.find("[data-field]").each(function() { @@ -409,6 +415,7 @@ async function onModalSubmit($modal, $table, url) { return; } + clearValidation($modal); let data = { server: selectedServer.id }; $modal.find("[data-field]").each(function() { @@ -446,7 +453,26 @@ async function onModalSubmit($modal, $table, url) { $modal.modal("hide"); $table.trigger("doDataLoad"); }) - .catch(error => logError(error)); + .catch(error => { + logError(error); + if (typeof error === "object" && "responseJSON" in error) { + renderErrorMessages($modal, error.responseJSON); + } + }); +} + + +// region Modal Error Msgs + +function renderErrorMessages($modal, errorObj) { + for (const key in errorObj) { + const value = errorObj[key]; + const $input = $modal.find(`[data-field="${key}"]`); + $input.addClass("is-invalid"); + $input.next(".form-text").after( + `
${value}
` + ) + } } diff --git a/apps/home/templates/home/modals/editSub.html b/apps/home/templates/home/modals/editSub.html index 72a7f30..0445384 100644 --- a/apps/home/templates/home/modals/editSub.html +++ b/apps/home/templates/home/modals/editSub.html @@ -1,7 +1,7 @@