At dialog level how to restrict pathfield that should accept only .mp4 or .mov extensions files from dam | Community
Skip to main content
Level 2
May 28, 2024
Solved

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

  • May 28, 2024
  • 3 replies
  • 994 views

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.

 

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

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.

 

3 replies

sravs
Community Advisor
Community Advisor
May 28, 2024
HrishikeshKagne
Community Advisor
HrishikeshKagneCommunity AdvisorAccepted solution
Community Advisor
May 28, 2024

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.

 

Hrishikesh Kagane
kautuk_sahni
Community Manager
Community Manager
June 10, 2024

@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