Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

AEM Cloud Service - Assets Metadata Editor Multifield Limit Validation | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

AEM Cloud Service - Assets Metadata Editor Multifield Limit Validation by Sreekanth Choudry Nalabotu

Abstract

Goal
Add a Client Side Validator for limiting the number of items in a Multifield in Assets Metadata Editor

Min/max limit configuration is hardcoded in the extension. For making it configurable via schema model check this post

Solution
Create a client library /apps/eaem-cs-meta-limit-mf/clientlibs/clientlib-limit-mf with categories=dam.gui.coral.metadataeditor and dependencies= eaem.lodash. Add the following js logic for validator in /apps/eaem-cs-meta-limit-mf/clientlibs/clientlib-limit-mf/meta-limit-mf.js

(function($, $document){
var DATA_MF_NAME = "data-granite-coral-multifield-name",
MF_NAME = "./jcr:content/metadata/eaemKeywords",
EAEM_VALIDATION_ERROR = "eaem-assets-validation-error",
EAEM_MAX_ITEMS = 5,
EAEM_MIN_ITEMS = 1, continueSave = true;

$document.on("foundation-contentloaded", validateMF);

function validateMF(){
var $multifield = $("[" + DATA_MF_NAME + "='" + MF_NAME + "']");

addValidator($multifield, EAEM_MAX_ITEMS, EAEM_MIN_ITEMS);

var validation = $multifield.adaptTo("foundation-validation");

validation.checkValidity();

validation.updateUI();

$("#shell-propertiespage-doneactivator").click(function(event){
if(continueSave){
return;
}

event.preventDefault();
event.stopPropagation();

showAlert("error", "Error", "Validation failed");
})
}

function addValidator($multifield, maxItems, minItems){
if(maxItems){
maxItems = parseInt(maxItems);
}

if(minItems){
minItems = parseInt(minItems);
}

$multifield.attr("aria-required","true");

var registry = $(window).adaptTo("foundation-registry");

registry.register("foundation.validation.validator", {
selector: "#" + $multifield.attr("id"),
validate: validate
});

function validate(){
var count = $multifield[0]._items.length;

continueSave = true;

if(maxItems && (count > maxItems) ){
continueSave = false;
return "Maximum allowed : " + maxItems + " items";
}

if(minItems && (count < minItems) ){
continueSave = false;
return "Minimum required : " + minItems + " items";
}

return false;
}
}

function showAlert(variant, header, content) {
var $dialog = $("#" + EAEM_VALIDATION_ERROR), dialog;

if (!_.isEmpty($dialog)) {
$dialog[0].show();
return;
}

dialog = new Coral.Dialog().set({
id: EAEM_VALIDATION_ERROR,
variant: variant,
closable: "on",
header: {
innerHTML: header
},
content: {
innerHTML: content
},
footer: {
innerHTML: ''
}
});

document.body.appendChild(dialog);

dialog.show();
}
}(jQuery, jQuery(document)));

Read Full Blog

AEM Cloud Service - Assets Metadata Editor Multifield Limit Validation

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies