Content Fragment Component Validation | Community
Skip to main content
Level 3
September 21, 2021
Solved

Content Fragment Component Validation

  • September 21, 2021
  • 3 replies
  • 3122 views

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. 

 

 

 

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

3 replies

Anny0505
Community Advisor
Community Advisor
September 21, 2021

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

 

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

NitinLAuthor
Level 3
September 21, 2021

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

Any references for JS validation for CF component?

Anny0505
Community Advisor
Anny0505Community AdvisorAccepted solution
Community Advisor
September 21, 2021
ibishika
Level 4
September 21, 2021

@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

NitinLAuthor
Level 3
September 21, 2021

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

 

ibishika
Level 4
September 21, 2021

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

Kishore_Kumar_
Level 9
September 22, 2021

Hi @nitinl ,

 

PFB MVP solution for this.

 

 //validaton property.

 

 

//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());	
	}
}

 

January 31, 2024

is there any property so we can verify that our content fragment is saved not publish as i have to require the value when we save the content fragment