An approach for server side validation in Cq5

While working with cq5 forms you would need to do some validations at server side end. These validations can be very generic like character only validation or may be complex business scenarios. The approach we would discuss would incorporate all these scenarios:

The first and foremost thing to do is to override the default server validation file in /libs directory (/libs/foundation/components/form/defaults/field/servervalidation.jsp) into your project like /apps/foundation/components/form/defaults/field/servervalidation.jsp

The file above is called for each form field separately when the form is submitted. Within this default server validation field I suggest you to validate only required constraint. This can be achieved with the help of form api’s available OOTB.

Required validation (available OOTB in the constraint tab on the form field) can be done with this code below.

 FieldDescription[] descs = FieldHelper.getFieldDescriptions(slingRequest, resource);  
   for(final FieldDescription desc : descs) {  
 if ( desc.isRequired()) {  
         String[] values = request.getParameterValues(desc.getName());  
         // You can check for length of values in the string array/values whatever 
//suits you for required validation.  

Now, if required validation passes you might want to validate other constraints that can be applicable per form field like numbers/characters/business constraint for the form.

This can be done by creating constraints at location /apps/<your-project-name>/components/form/constraints/<constraint-name>/servervalidation.jsp. Similar to the ones created under /libs at the same location.

The sling:resourceType for the <constraint-name> would be foundation/components/form/constraint.

Now, the question arises how would you call server side validation jsp for specific constraints?

First you have to set the specific constraint in the dialog of the form field. Make sure that the constraint you just created above is available in the dialog. This can be achieved by writing a Servlet that produces a json. When the dialog is set for the respective form field you can call its serversidevalidation.jsp.

The code for this can be: (A short snippet of my project code, sorry can’t revel more)

 final Resource includeResource = new ExtendedResourceWrapper(desc.getFieldResource(), resourceType, FormsConstants.RST_FORM_CONSTRAINT);  
 FormsHelper.includeResource(slingRequest, slingResponse, includeResource, FormsConstants.SCRIPT_SERVER_VALIDATION);  

ExtendedResourceWrapper class is nothing but extends the and has just a constructor that accepts resource, resourceType and resourceSuperType)

By including these scripts below the required validation (in default server validation file) , you would be able to call the validation (servervalidaiton.jsp) file that has been configured over the form element.

Now, you can retrieve the value of the form fields as we did in required validation and do pattern matchings/ can apply complex business validation rules.

If you face problems in implementing the above approach, please leave a comment and I would be happy to help 🙂


One thought on “An approach for server side validation in Cq5

  1. I’ve to create a CONDITIONAL REQUIRED option (the field is required if a radio option is selected), so I uncheck the required flag from the field component and created my servervalidation.jsp. But when I write nothing inside the field, the servervalidation,jsp seems not to be called. But if I put the required flag it stops on the required check before my servervalidation. Is there a way to rewrite the Required logic?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s