AEM – Custom Validation Concept

Custom validation in AEM allows you to define and enforce business rules for content entered by authors. AEM provides a variety of validation options out of the box, but sometimes you may need to create your own custom validation logic to meet specific business requirements.

Here are the steps to create custom validation in AEM:

  1. Define the validation logic: Decide what kind of validation you need to perform. For example, you may want to validate that a certain field is not empty, that a number is within a specific range, or that a date is in a valid format.
  2. Create a validation handler: In AEM, you can create a custom validation handler by implementing the “com.adobe.granite.ui.validation.Validator” interface. This interface defines a single method, “validate()”, which takes the current component context and returns a list of validation messages if there are errors.

Here is an example implementation of a custom validation handler:

@Component(service = Validator.class, property = {
        Validator.NAME + "=" + "my-custom-validator",
        Validator.TYPE + "=" + Validator.TYPE_DEFAULT,
        Validator.ADAPTABLE_RESOURCE_TYPES + "=" + "my-project/components/my-component"
public class MyCustomValidator implements Validator {
    public List<ValidationResult> validate(ComponentContext componentContext) {
        List<ValidationResult> validationResults = new ArrayList<>();
        // add your validation logic here
        return validationResults;

In this example, we create a new OSGi component that implements the “Validator” interface. We specify a unique name for our validator, the type of validator, and the resource type of the component that we want to apply the validation to.

The “validate()” method takes a “ComponentContext” object as an argument. This object provides access to the current request, the current resource, and the current form data. The method returns a list of “ValidationResult” objects. If the list is empty, then the validation succeeded. Otherwise, the list contains validation messages that describe the errors.

  1. Add the validation handler to your AEM project: To add your custom validator to your AEM project, you need to create an OSGi bundle that includes your validator class. You can then deploy the bundle to AEM using the Felix Console or Maven.
  2. Apply the validation to your component: Once you have deployed your custom validator to AEM, you can apply it to your component by adding the “validation” attribute to the dialog field that you want to validate. For example:

In this example, we add the “validation” attribute to a text field in our dialog. The value of the attribute is the name of our custom validator.

When an author submits the dialog, AEM will run the validation logic defined in the custom validator. If there are errors, AEM will display the validation messages to the author. If there are no errors, AEM will save the content as usual.

I hope this explanation helps you understand how to create custom validation in AEM.

Trending Topics

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial