Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Content Fragment Component Validation

Avatar

Level 3

Hi All, 

 

Is there any configuration available in content fragment component to validate content fragment field, such as if user is not selecting content fragment from available fragments in drop down and clicking on done button, he/she will get notification text( ie: fragment not selected)?

Currently OOB CF component allows user to submit the component on folder only selection. 

contentfrag.png

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
0 Replies

Avatar

Community Advisor

@NitinL ,

 

I don't think any such feature is available in current CF component. While browsing/selecting the CF from the list it is pointing to CF location so that Content Editor can choose from the list.

 

 if you want to implement anything extra then you have to customize it. My advice is to train authors to choose the CF from the desired location.

ArunaSurukunta_0-1632240711991.png

 

Yo can implement the JS to validate the path that is entered by the editor and if it is not CF path then do not allow author to submit the dialog.

Thanks,

Aruna

Avatar

Level 3

Thanks @Aruna_surukunta_ , somehow authors are frequently missing to select CF, resulting unwanted issues:(. 

Any references for JS validation for CF component?

Avatar

Correct answer by
Community Advisor

Avatar

Level 5

@NitinL 

 

Follow the below steps:

1. Overlay the contentfragment dialog from libs to apps. You can find the core component in this path: /libs/core/wcm/components/contentfragment/v1/contentfragment/cq:dialog/

2. Add a property "required" "Boolean" "true" to the fragmentPath field(/libs/core/wcm/components/contentfragment/v1/contentfragment/cq:dialog/content/items/tabs/items/properties/items/column/items/fragmentPath).

 

required    Boolean    true

Avatar

Level 3

Thanks @ibishika, required=true will not work, in given use case user would enter folder path /content/dam/test/ and this validation would fail. 

 

Avatar

Level 5

@NitinL 

Sorry, I misunderstood the question. In this case you will have to go with a custom validation script. This might take more effort than sounds. I think, you will have to check the resourceType from backend code and use jquery to fetch that and validate.

Avatar

Level 3

no worries, can you please share any reference implementation please.. example etc.. 

Avatar

Community Advisor

Hi @NitinL ,

 

PFB MVP solution for this.

 

 //validaton property.

 

kishorekumar14_0-1632284531178.png

 

//script to validate the CF path

 

$(window).adaptTo("foundation-registry").register("foundation.validation.validator", {
	  selector: "[data-foundation-validation='fragmentpath.validation']",
	  validate: function(el) {
        var fragmentPath = el.value;
        var message = "";
   		$.ajax({
            url: "/bin/validateFragmentPath.json?fragmentPath="+fragmentPath,
            async: false,
            success: function(data) {
               if(!data.hasOwnProperty('success')){
		 message = "Invalid content path";
	        }              
            }
        });
        return message;
	  }
});

Servlet call to check if its a valid CF,

 

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletPaths;
import org.osgi.service.component.annotations.Component;
import com.adobe.cq.dam.cfm.ContentFragment;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Component(service = {Servlet.class})
@SlingServletPaths(value="/bin/validateFragmentPath.json")
public class CFValidationServlet extends SlingSafeMethodsServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(final SlingHttpServletRequest request,
			final SlingHttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		String fragmentPath = request.getParameter("fragmentPath");
		
		ObjectMapper mapper = new ObjectMapper();
		ObjectNode keyNode = mapper.createObjectNode();
		
		if(StringUtils.isNotEmpty(fragmentPath)) {
			ResourceResolver resolver = request.getResourceResolver();
			Resource resource = resolver.getResource(fragmentPath);
			if(resource != null) {
				ContentFragment fragment = resource.adaptTo(ContentFragment.class);
				if(fragment != null) {
					keyNode.put("success", "valid content fragment");
				}
			}
		}		
		response.getWriter().print(keyNode.toPrettyString());	
	}
}