Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

At dialog level how to restrict pathfield that should accept only .mp4 or .mov extensions files from dam

Avatar

Level 1

suppose i am dropping video component on page. while authoring that component i am choosing video path from dam as pathfield but it is supporting all extensions file type .i have to restrict it should support only .mp4 or mov file type. how to do that thing in AEM. As i am new to AEM please help me on this.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @prachiat ,

To restrict a pathfield in an AEM dialog to accept only .mp4 or .mov file extensions from DAM, you can use a custom validator. Here's how you can achieve this:

  1. Open the dialog XML file for your video component. This file is typically located in the /apps folder of your AEM project.

  2. Locate the pathfield element in the dialog XML file that represents the video path selection.

  3. Add a validator property to the pathfield element and set it to the path of your custom validator. For example:

     

 

<pathfield
    jcr:primaryType="nt:unstructured"
    fieldLabel="Video Path"
    name="./videoPath"
    validator="myproject.validators.VideoPathValidator"
/>

 

Create a new Java class for your custom validator. In this example, the class is named VideoPathValidator. This class should implement the Validator interface and override the validate method. Here's an example implementation:

 

package myproject.validators;

import com.adobe.granite.ui.components.Value;
import com.adobe.granite.ui.components.Validator;
import org.apache.commons.lang3.StringUtils;

public class VideoPathValidator implements Validator {
    private static final String[] ALLOWED_EXTENSIONS = {".mp4", ".mov"};

    @Override
    public String validate(Value value) {
        String path = value.getString();
        if (StringUtils.isNotBlank(path)) {
            String extension = StringUtils.substringAfterLast(path, ".");
            if (!isValidExtension(extension)) {
                return "Invalid file extension. Only .mp4 and .mov files are allowed.";
            }
        }
        return null;
    }

    private boolean isValidExtension(String extension) {
        for (String allowedExtension : ALLOWED_EXTENSIONS) {
            if (allowedExtension.equalsIgnoreCase(extension)) {
                return true;
            }
        }
        return false;
    }
}

 

  1. In this example, the validate method checks if the selected file path has a valid extension. If the extension is not .mp4 or .mov, it returns an error message.

  2. Build and deploy your project to see the changes take effect.

With this implementation, the pathfield in your AEM dialog will only accept .mp4 or .mov file extensions from DAM. If any other file extension is selected, an error message will be displayed.

 

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @prachiat ,

To restrict a pathfield in an AEM dialog to accept only .mp4 or .mov file extensions from DAM, you can use a custom validator. Here's how you can achieve this:

  1. Open the dialog XML file for your video component. This file is typically located in the /apps folder of your AEM project.

  2. Locate the pathfield element in the dialog XML file that represents the video path selection.

  3. Add a validator property to the pathfield element and set it to the path of your custom validator. For example:

     

 

<pathfield
    jcr:primaryType="nt:unstructured"
    fieldLabel="Video Path"
    name="./videoPath"
    validator="myproject.validators.VideoPathValidator"
/>

 

Create a new Java class for your custom validator. In this example, the class is named VideoPathValidator. This class should implement the Validator interface and override the validate method. Here's an example implementation:

 

package myproject.validators;

import com.adobe.granite.ui.components.Value;
import com.adobe.granite.ui.components.Validator;
import org.apache.commons.lang3.StringUtils;

public class VideoPathValidator implements Validator {
    private static final String[] ALLOWED_EXTENSIONS = {".mp4", ".mov"};

    @Override
    public String validate(Value value) {
        String path = value.getString();
        if (StringUtils.isNotBlank(path)) {
            String extension = StringUtils.substringAfterLast(path, ".");
            if (!isValidExtension(extension)) {
                return "Invalid file extension. Only .mp4 and .mov files are allowed.";
            }
        }
        return null;
    }

    private boolean isValidExtension(String extension) {
        for (String allowedExtension : ALLOWED_EXTENSIONS) {
            if (allowedExtension.equalsIgnoreCase(extension)) {
                return true;
            }
        }
        return false;
    }
}

 

  1. In this example, the validate method checks if the selected file path has a valid extension. If the extension is not .mp4 or .mov, it returns an error message.

  2. Build and deploy your project to see the changes take effect.

With this implementation, the pathfield in your AEM dialog will only accept .mp4 or .mov file extensions from DAM. If any other file extension is selected, an error message will be displayed.

 

Avatar

Administrator

@prachiat Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni