Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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? 

Topics

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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. 

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

@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.