Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Alert message if page name exists

Avatar

Level 3

On page creation, page move, etc., can an alert somehow be added to warn the author the page name already exists instead of AEM creating a page variation with a number at the end?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @JonMaguire 

 For PageName property, you can add new custom validator. Normally for this you have "foundation.jcr.name" and regex already attached but you want to put custom validator , you can do it.

 

example- This is validator for page name regex and your "validation" property looks like " foundation.jcr.name,pagename-regex"

(function(window, $, Granite) {
    "use strict";
var NON_VALID_CHARS = "^([a-z0-9-_])*$";
$(window).adaptTo("foundation-registry").register("foundation.validation.validator", {
    selector: "[data-validation~='pagename-regex']",
    validate (el) {
        var pattern = new RegExp(NON_VALID_CHARS);
        var value = el.value;
        for (var i = 0, ln = value.length; i < ln; i++) {
                if (!pattern.test(value[i]) ) {
                    return Granite
                    .I18n.get("Invalid character '{0}'. It must be a valid JCR name (only lowercase letters, numbers, dashes and underscores are allowed).", [ value[i] ]);
                }
            }
    }
});
    var pageNamePattern = /^[a-z0-9_-]+$/;
    $(window).adaptTo("foundation-registry").register("foundation.validation.validator", {
        selector: '[data-foundation-validation~="admin.pagename"]',
        validate (el) {
            var valid = pageNamePattern.test(el.value) || el.value === "";

            if (!valid) {
                return Granite.I18n.get("This field must only contain lowercase letters, numbers, dashes and underscores.");
            }
        }
    });
})(window, Granite.$, Granite);

  

View solution in original post

2 Replies

Avatar

Level 7

consider using workflow to get triggered on page event to validate page name on current or destination content tree 

Avatar

Correct answer by
Community Advisor

Hi @JonMaguire 

 For PageName property, you can add new custom validator. Normally for this you have "foundation.jcr.name" and regex already attached but you want to put custom validator , you can do it.

 

example- This is validator for page name regex and your "validation" property looks like " foundation.jcr.name,pagename-regex"

(function(window, $, Granite) {
    "use strict";
var NON_VALID_CHARS = "^([a-z0-9-_])*$";
$(window).adaptTo("foundation-registry").register("foundation.validation.validator", {
    selector: "[data-validation~='pagename-regex']",
    validate (el) {
        var pattern = new RegExp(NON_VALID_CHARS);
        var value = el.value;
        for (var i = 0, ln = value.length; i < ln; i++) {
                if (!pattern.test(value[i]) ) {
                    return Granite
                    .I18n.get("Invalid character '{0}'. It must be a valid JCR name (only lowercase letters, numbers, dashes and underscores are allowed).", [ value[i] ]);
                }
            }
    }
});
    var pageNamePattern = /^[a-z0-9_-]+$/;
    $(window).adaptTo("foundation-registry").register("foundation.validation.validator", {
        selector: '[data-foundation-validation~="admin.pagename"]',
        validate (el) {
            var valid = pageNamePattern.test(el.value) || el.value === "";

            if (!valid) {
                return Granite.I18n.get("This field must only contain lowercase letters, numbers, dashes and underscores.");
            }
        }
    });
})(window, Granite.$, Granite);