Content Fragment: Custom Content Reference Field Validation (AEM 6.4) | Community
Skip to main content
Level 2
March 22, 2021
Solved

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

  • March 22, 2021
  • 1 reply
  • 2614 views

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? 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BrianKasingli

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

1 reply

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
March 22, 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. 

kautuk_sahni
Community Manager
Community Manager
March 23, 2021
@briankasingli, good to see you back 🙂
Kautuk Sahni