Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM 6.5 textfield Unique validation issue

Avatar

Level 4

I'm new to AEM development and got a requirement to extend the Textfield Unique validation for all Content fragment models to work in a case-insensitive manner. Like it should consider the Person and person as the same and show a unique value error message. Can anyone please suggest if it is possible to do this by making some configuration changes or how can we do this customization? Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @MukeshAEM,

Unfortunately this is not configurable.

In general Unique validation option is handled by com.adobe.cq.dam.cfm.impl.servlets.UniqueFieldValidatorServlet.

UniqueFieldValidatorServlet.jpg

As it can be seen on above screen shot UniqueFieldValidatorServlet is registered on specific extension, resourceType and support GET method.

What you can do, is to create your own custom servlet with your logic that will be used instead of OOTB one. Below is sample code snippet. Make sure to set high ranking - this will guarantee your servlet will be used instead of OOTB.

@Component(service = { Servlet.class })
@SlingServletResourceTypes(
        resourceTypes="dam/cfm/editor/validator/uniquefield",
        methods= HttpConstants.METHOD_GET,
        extensions="json")
// setting high ranking value to make sure custom servlet will be used instead of OOTB
@ServiceRanking(Integer.MAX_VALUE)
public class CustomUniqueFieldValidatorServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doGet(final SlingHttpServletRequest req,
            final SlingHttpServletResponse resp) throws ServletException, IOException {
            // place for your code
    }
}

Your custom servlet has to consume below 3 params:

  • fragmentPath - path to currently edited content fragment
  • fieldName - represents filed name in which you would like to check uniqueness
  • value - represents value that will be checked if it is unique

Above params will be passed automatically to your servlet as part of GET request query string.

Please also be aware that com.adobe.cq.dam.cfm.impl.servlets.UniqueFieldValidatorServlet is not exported so you will not be able to extend this servlet directly and use code from it. You have to build entire logic by yourself.

Last but not least, if you decide to write your own servlet it has to return results in proper json format, here is an example

{
	"fieldName":"title",
	"fragmentPath":"/content/dam/test/my-cf-2",
	"value":"Adobe",
	"isValid":false,
	"references":[
		{
			"path":"/content/dam/test/my-cf-1",
			"title":"my-cf-1"
		}
	]
}

You can also check format of result json and request parameters using web tools in your browser.

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @MukeshAEM,

Unfortunately this is not configurable.

In general Unique validation option is handled by com.adobe.cq.dam.cfm.impl.servlets.UniqueFieldValidatorServlet.

UniqueFieldValidatorServlet.jpg

As it can be seen on above screen shot UniqueFieldValidatorServlet is registered on specific extension, resourceType and support GET method.

What you can do, is to create your own custom servlet with your logic that will be used instead of OOTB one. Below is sample code snippet. Make sure to set high ranking - this will guarantee your servlet will be used instead of OOTB.

@Component(service = { Servlet.class })
@SlingServletResourceTypes(
        resourceTypes="dam/cfm/editor/validator/uniquefield",
        methods= HttpConstants.METHOD_GET,
        extensions="json")
// setting high ranking value to make sure custom servlet will be used instead of OOTB
@ServiceRanking(Integer.MAX_VALUE)
public class CustomUniqueFieldValidatorServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doGet(final SlingHttpServletRequest req,
            final SlingHttpServletResponse resp) throws ServletException, IOException {
            // place for your code
    }
}

Your custom servlet has to consume below 3 params:

  • fragmentPath - path to currently edited content fragment
  • fieldName - represents filed name in which you would like to check uniqueness
  • value - represents value that will be checked if it is unique

Above params will be passed automatically to your servlet as part of GET request query string.

Please also be aware that com.adobe.cq.dam.cfm.impl.servlets.UniqueFieldValidatorServlet is not exported so you will not be able to extend this servlet directly and use code from it. You have to build entire logic by yourself.

Last but not least, if you decide to write your own servlet it has to return results in proper json format, here is an example

{
	"fieldName":"title",
	"fragmentPath":"/content/dam/test/my-cf-2",
	"value":"Adobe",
	"isValid":false,
	"references":[
		{
			"path":"/content/dam/test/my-cf-1",
			"title":"my-cf-1"
		}
	]
}

You can also check format of result json and request parameters using web tools in your browser.