Content Fragment: Custom Content Reference Field Validation (AEM 6.4)

Avatar

Avatar

AEMRand

Avatar

AEMRand

AEMRand

22-03-2021

Hello, 

I have a content fragment model that leverages a content reference field as such...

 

 

<_x0031_597244630515
                            jcr:primaryType="nt:unstructured"
                            sling:resourceType="dam/cfm/models/editor/components/contentreference"
                            fieldLabel="Product Name"
                            listOrder="27"
                            metaType="reference"
                            name="richproductName"
                            nameSuffix="contentReference"
                            required="on"
                            renderReadOnly="false"
                            showEmptyInReadOnly="true"
                            valueType="string"/>

 

 

I see that with the addition of the rootPath attribute, I can limit the directory to where a reference can be made. However, this doesnt stop someone from linking to something in the directory that shouldn't be there (say if someone creates an invalid experience fragment.) Is there a way to add some kind of validation/code that I can run when someone selects something from the content reference field? 

6.4 content fragment content reference

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

BrianKasingli

MVP

Avatar

BrianKasingli

MVP

BrianKasingli
MVP

22-03-2021

@AEMRand 

It is possible, I have provided a solution, please take a look here -> https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/ootb-validation-options-fo...

}}

In short, you can use JavaScript to append the "pattern" attribute to a given HTML element, from there it will enable HTML validation for the content fragment field itself; this is a regex pattern. Your client library should have a clientlib category of "dam.cfm.authoring.v2", so when you are on the cfm authoring view, your code will be run.

So you can imagine, anything that does not match ^/content/dam/my-site.+, then on-keyup, validation will fail, and the input field will be highlighted in red. 

Answers (0)