Maxlength custom validator for Richtext using Granite UI Jquery | AEMaaCs | AEM Community Blog Seeding | Adobe Higher Education
Skip to main content
kautuk_sahni
Community Manager
Community Manager
August 2, 2022

Maxlength custom validator for Richtext using Granite UI Jquery | AEMaaCs | AEM Community Blog Seeding

  • August 2, 2022
  • 0 답변들
  • 372 조회

BlogImage.jpg

Maxlength custom validator for Richtext using Granite UI Jquery | AEMaaCs by Aemsimplifiedbynikhil

Abstract

As we all know that when it comes to apply the maxlegth property to a richtext field in AEM, it doesn’t actually work. Because RTE in dialog uses the concept of validation from the Form Validation in Granite UI. And, since, Inline-editing does not behave as a form, it can’t use that validation concept.

So we will make use of Granite UI api to create our custom validator which will help us to add validation of maxlength even on the richtext field in AEM TouchUI.

Create a validator.js in clientlibs with following Jquery: Here we are targeting the richtext-container and inside that container we are targeting a class that we will provide to the richtext field i.e rich-custom using Granite UI Api.


;(function (window, $) {
'use strict';

var RichTextMaxLengthValidation= function () {
var CONST = {
TARGET_GRANITE_UI: '.coral-RichText-editable',
ERROR_MESSAGE: 'Your text length is {0} but character limit is {1}!',
};

function init() {
// register the validator which includes the validate algorithm
$(window).adaptTo('foundation-registry').register('foundation.validation.validator', {
selector: CONST.TARGET_GRANITE_UI,
validate: function (el) {
var $rteField = $(el);
var $field = $rteField.closest('.richtext-container').find('input.rich-custom');
var maxLength = $field.data('maxlength');
var textLength = $rteField.text().trim().length;
if (maxLength && textLength > maxLength) {
return Granite.I18n.get(CONST.ERROR_MESSAGE, [textLength, maxLength]);
}
return null;
}
});
// execute Jquery Validation onKeyUp
$(document).on('keyup', CONST.TARGET_GRANITE_UI, function (e) {
executeJqueryValidation($(this));
});
}

function executeJqueryValidation(el) {
var validationApi = el.adaptTo('foundation-validation');
if (validationApi) {
validationApi.checkValidity();
validationApi.updateUI();
}
}
return {
init: init
}
}();
RichTextMaxLengthValidation.init();
})(window, Granite.$);

Read Full Blog

Maxlength custom validator for Richtext using Granite UI Jquery | AEMaaCs

Q&A

Please use this thread to ask the related questions.

이 주제는 답변이 닫혔습니다.