From c46576de5263588801045c6aa8950f8cbfe4f3cc Mon Sep 17 00:00:00 2001 From: mheil Date: Thu, 7 Mar 2024 08:15:30 +0100 Subject: [PATCH] #43 restored original functionality - fixed update view with new spectrum selectors - workaround for update ui race condition with 0.2 sec delay due to false handling for error message update by OOTB validation handler --- .../clientlibs/js/validations.js | 61 +++++++++++++++++-- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/main/resources/SLING-INF/apps/merkle/genericmultifield/clientlibs/js/validations.js b/src/main/resources/SLING-INF/apps/merkle/genericmultifield/clientlibs/js/validations.js index 3b32cf7..2725567 100644 --- a/src/main/resources/SLING-INF/apps/merkle/genericmultifield/clientlibs/js/validations.js +++ b/src/main/resources/SLING-INF/apps/merkle/genericmultifield/clientlibs/js/validations.js @@ -26,7 +26,7 @@ // register adapter for generic multi-field registry.register("foundation.adapters", { type: "foundation-field", - selector: ".coral-GenericMultiField", + selector: Merkle.Helper.CONST.CORAL_GENERIC_MULTIFIELD_SELECTOR, adapter: function (el) { var $el = $(el); return { @@ -62,14 +62,65 @@ // register selector for generic multi-field registry.register("foundation.validation.selector", { - submittable: ".coral-GenericMultiField", + submittable: Merkle.Helper.CONST.CORAL_GENERIC_MULTIFIELD_SELECTOR, candidate: ".coral-GenericMultiField:not([disabled]):not([data-renderreadonly=true])", exclusion: ".coral-GenericMultiField *" }); - // perform validation every time generic multi-field changed - $(document).on("change", ".coral-GenericMultiField", function () { + // register validator for generic multi-field + registry.register("foundation.validation.validator", { + selector: Merkle.Helper.CONST.CORAL_GENERIC_MULTIFIELD_SELECTOR, + validate: function (el) { + var $field = $(el).closest(".coral-Form-field"), items = $field.find(".coral-GenericMultiField-list li"), + minElements = $field.data("minelements"), maxElements = $field.data("maxelements"); + + // validate required attribute + if ($field.adaptTo("foundation-field").isRequired() && items.length === 0) { + return Granite.I18n.get(Merkle.Helper.CONST.ERROR_MESSAGE_REQUIRED); + + } + + // validate min and max elements (only if field is required) + if ($field.adaptTo("foundation-field").isRequired()) { + // validate if minElements restriction is met + if (items && !isNaN(minElements) && items.length < minElements) { + return Granite.I18n.get(Merkle.Helper.CONST.ERROR_MESSAGE_MIN, minElements); + } + // validate if maxElements restriction is met + if (items && !isNaN(maxElements) && items.length > maxElements) { + return Granite.I18n.get(Merkle.Helper.CONST.ERROR_MESSAGE_MAX, maxElements); + + } + } + + return null; + }, + show: function (el, message, ctx) { + var $field = $(el).closest(".coral-Form-field"); + $field.adaptTo("foundation-field").setInvalid(true); + + setTimeout(function() { + $field.siblings(".coral-Form-errorlabel").each(function (index, element) { + if (index > 0) { + element.remove() + } + }); + }, 200); + + ctx.next(); + }, + clear: function (el, ctx) { + var $field = $(el).closest(".coral-Form-field"); + $field.adaptTo("foundation-field").setInvalid(false); + $field.siblings(".coral-Form-fielderror").remove(); + $field.siblings(".coral-Form-errorlabel").remove(); + ctx.next(); + } + }); + + // perform validation every time generic multifield changed + $(document).on("change", Merkle.Helper.CONST.CORAL_GENERIC_MULTIFIELD_SELECTOR, function () { _performValidation($(this)); }); -})(window, Granite.$, CUI); \ No newline at end of file +})(window, Granite.$, CUI);