Expand my Community achievements bar.

Get ready! An upgraded Experience League Community experience is coming in January.

AEM : Custom metadata validation

Avatar

Level 3

I've created a custom metadata-schema which has some text fields, dropdowns and tag pickers. For one of the text fields, I want to apply a regex that will be validating the input which should be a string - 7 characters max, can only be letters and numbers (no characters), and no spaces. I've mapped the field to property ./jcr:content/metadata/shortcut-key

 

Accordingly I've created a clientlib under apps with category : dam.gui.coral.metadataeditor

ac4320_2-1766586300021.png

 

The hierarchy is as follows :
\-- metadata-validation (cq:ClientLibraryFolder)
    \-- js.txt (nt:file)
    \-- js (nt:folder)
       \-- metadataValidation.js (nt:file)

 

The JS file has the following code :

(function($) {
    $(window).adaptTo("foundation-registry").register("foundation.validation.validator", {

        selector: '[name="./jcr:content/metadata/shortcut-key"]',
        validate: function(element) {

            const wrapper = $(element).parents().eq(1);

            // Apply only if schema is custom-schema
            if(!wrapper 
                || !wrapper.attr('data-path') 
                || !wrapper.attr('data-path').startsWith('/conf/global/settings/dam/adminui-extension/metadataschema/custom-schema')) return;

            const validationRegex = /^[A-Za-z0-9]{1,7}$/;

            if (!validationRegex.test(element.value)) {
                return Granite.I18n.get('String — 7 characters max, can only be letters and numbers (no special characters, no spaces)');
            }
        }
    });
})(Granite.$);

 
Now the issue is although the validations are working - it allows to save the metadata schema along with validation errors.

ac4320_0-1766585939913.png
ac4320_1-1766585961682.png

 

As per https://medium.com/@theopendle/aem-custom-metadata-forms-for-assets-8c6597205f9d - it should throw the pop-up text and not allow to save the form.

Can someone let me know what is the issue over here? And how to prohibit the form from getting submitted when there are validation errors.

 

1 Reply

Avatar

Level 2

Hi @ac4320 

The regex and clientlib setup look fine. The reason you’re seeing the validation message but the metadata still gets saved is because, in the DAM Metadata Editor, showing a validation error alone doesn’t block the save.

 

In your validator, you’re only returning a message from validate(). That’s enough to display the error inline, but the DAM editor will still allow the save unless the field is explicitly marked as invalid.

 

Internally, the DAM save checks whether any field has aria-invalid="true". Since that state is never set, the editor treats the form as valid and proceeds with the save.

 

Once you mark the field invalid (and clear it again when the value is valid), the save action will be blocked as expected.