Skip to content

Commit ac35d0c

Browse files
added support for new extesnions in the file input with test cases
1 parent e8a54f3 commit ac35d0c

File tree

12 files changed

+103
-43
lines changed

12 files changed

+103
-43
lines changed

bundles/af-core/src/main/java/com/adobe/cq/forms/core/components/internal/form/ReservedProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private ReservedProperties() {
123123
public static final String PN_UNCHECKED_VALUE = "uncheckedValue";
124124
public static final String PN_MAX_FILE_SIZE = "maxFileSize";
125125
public static final String PN_FILE_ACCEPT = "accept";
126+
public static final String PN_FILE_ACCEPT_EXTENSIONS = "acceptExtensions";
126127
public static final String PN_BUTTON_TEXT = "buttonText";
127128
public static final String PN_WRAP_DATA = "wrapData";
128129
public static final String PN_FRAGMENT_PATH = "fragmentPath";

bundles/af-core/src/main/java/com/adobe/cq/forms/core/components/internal/models/v1/form/FileInputImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public class FileInputImpl extends AbstractFieldImpl implements FileInput {
5757
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_FILE_ACCEPT)
5858
protected String[] accept;
5959

60+
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_FILE_ACCEPT_EXTENSIONS)
61+
protected String[] acceptExtensions;
62+
6063
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = ReservedProperties.PN_BUTTON_TEXT)
6164
@Default(values = FileInput.DEFAULT_BUTTON_TEXT)
6265
protected String buttonText;
@@ -122,6 +125,13 @@ public List<String> getAccept() {
122125
.orElse(Collections.emptyList());
123126
}
124127

128+
@Override
129+
public List<String> getAcceptExtensions() {
130+
return Optional.ofNullable(acceptExtensions)
131+
.map(Arrays::asList)
132+
.orElse(Collections.emptyList());
133+
}
134+
125135
@Override
126136
public @NotNull Map<String, Object> getProperties() {
127137
Map<String, Object> customProperties = super.getProperties();

bundles/af-core/src/main/java/com/adobe/cq/forms/core/components/models/form/FileConstraint.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ default List<String> getAccept() {
5757
return DEFAULT_ACCEPT;
5858
}
5959

60+
default List<String> getAcceptExtensions() {
61+
return Collections.emptyList();
62+
}
63+
6064
/**
6165
* The constraint is
6266
* applicable for file attachment field. If the type is {@code String}, the format will always be {@code data-url}.

it/content/src/main/content/jcr_root/content/forms/af/core-components-it/samples/fileinput/fileinputv2/basic/.content.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,6 @@
8787
textIsRich="[true,true]"
8888
type="file[]"
8989
visible="{Boolean}true"/>
90-
<fileinput6
91-
jcr:primaryType="nt:unstructured"
92-
jcr:title="File Input - 6"
93-
sling:resourceType="core/fd/components/form/fileinput/v2/fileinput"
94-
accept="[audio/*, video/*, image/*, text/*, application/pdf,.ifc]"
95-
fieldType="file-input"
96-
readOnly="{Boolean}false"
97-
name="fileinput6"
98-
type="file"/>
9990
<submit
10091
jcr:lastModified="{Date}2023-01-17T16:28:58.844+05:30"
10192
jcr:lastModifiedBy="admin"

it/content/src/main/content/jcr_root/content/forms/af/core-components-it/samples/fileinput/fileinputv3/basic/.content.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@
103103
maxFileSize="2"
104104
type="file[]"
105105
visible="{Boolean}true"/>
106+
<fileinput7
107+
jcr:primaryType="nt:unstructured"
108+
jcr:title="File Input - 7"
109+
sling:resourceType="core/fd/components/form/fileinput/v2/fileinput"
110+
accept="[audio/*, video/*, image/*, text/*, application/pdf,.ifc]"
111+
fieldType="file-input"
112+
readOnly="{Boolean}false"
113+
name="fileinput7"
114+
type="file"/>
106115
<submit
107116
jcr:lastModified="{Date}2023-01-17T16:28:58.844+05:30"
108117
jcr:lastModifiedBy="admin"

ui.af.apps/src/main/content/jcr_root/apps/core/fd/components/form/fileinput/v1/fileinput/_cq_dialog/.content.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@
144144
jcr:primaryType="nt:unstructured"
145145
sling:resourceType="fd/af/authoring/components/granite/form/formsmultifield"
146146
fieldLabel="Allowed file types"
147-
fieldDescription="Specify the allowed file types using MIME types (for example, image/png, application/pdf) or file extensions (for example, .png, .pdf). Ensure that the specified extensions correspond to valid MIME types to avoid upload issues."
148147
defaultValue="">
149148
<field
150149
name="./accept"

ui.af.apps/src/main/content/jcr_root/apps/core/fd/components/form/fileinput/v3/fileinput/_cq_dialog/.content.xml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,37 @@
8787
</basic>
8888
<validation
8989
jcr:primaryType="nt:unstructured"
90-
sling:resourceType="granite/ui/components/coral/foundation/container"/>
90+
sling:resourceType="granite/ui/components/coral/foundation/container">
91+
<items jcr:primaryType="nt:unstructured">
92+
<columns
93+
jcr:primaryType="nt:unstructured"
94+
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"
95+
margin="{Boolean}true">
96+
<items jcr:primaryType="nt:unstructured">
97+
<column
98+
jcr:primaryType="nt:unstructured"
99+
sling:resourceType="granite/ui/components/coral/foundation/container">
100+
<items jcr:primaryType="nt:unstructured">
101+
<extensions
102+
jcr:primaryType="nt:unstructured"
103+
sling:resourceType="fd/af/authoring/components/granite/form/formsmultifield"
104+
fieldLabel="Allowed file extensions"
105+
fieldDescription="Specify the allowed file extensions (for example, .png, .pdf). Ensure that the specified extensions correspond to valid MIME types to avoid upload issues."
106+
defaultValue="">
107+
<field
108+
name="./acceptExtensions"
109+
jcr:primaryType="nt:unstructured"
110+
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
111+
</extensions>
112+
<acceptMessage
113+
jcr:primaryType="nt:unstructured"
114+
sling:resourceType="granite/ui/components/coral/foundation/form/textarea"/>
115+
</items>
116+
</column>
117+
</items>
118+
</columns>
119+
</items>
120+
</validation>
91121
<help
92122
jcr:primaryType="nt:unstructured"
93123
jcr:title="Help Content"

ui.af.apps/src/main/content/jcr_root/apps/core/fd/components/form/fileinput/v3/fileinput/fileinput.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
name="${file.name}"
5151
disabled="${!file.enabled}"
5252
required="${file.required}"
53-
accept="${file.accept}"
53+
accept="${[file.accept, file.acceptExtensions] @ join=','}"
5454
type="file"
5555
data-cmp-data-layer="${file.data.json}"
5656
/>

ui.frontend/src/view/FormFileInputWidgetBase.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -454,24 +454,24 @@ class FormFileInputWidgetBase {
454454
isCurrentInvalidFileName = false,
455455
isCurrentInvalidMimeType = false;
456456
currFileName = file.name.split("\\").pop();
457-
// Now size is in MB
458-
let size = file.size / 1024 / 1024;
459-
// check if file size limit is within limits
460-
if ((size > parseFloat(this.options.maxFileSize))) {
461-
isInvalidSize = isCurrentInvalidFileSize = true;
462-
inValidSizefileNames = currFileName + "," + inValidSizefileNames;
463-
} else if (!FileInputWidget.isValid(currFileName)) {
464-
// check if file names are valid (ie) there are no control characters in file names
465-
isInvalidFileName = isCurrentInvalidFileName = true;
466-
inValidNamefileNames = currFileName + "," + inValidNamefileNames;
467-
} else {
457+
// Now size is in MB
458+
let size = file.size / 1024 / 1024;
459+
// check if file size limit is within limits
460+
if ((size > parseFloat(this.options.maxFileSize))) {
461+
isInvalidSize = isCurrentInvalidFileSize = true;
462+
inValidSizefileNames = currFileName + "," + inValidSizefileNames;
463+
} else if (!FileInputWidget.isValid(currFileName)) {
464+
// check if file names are valid (ie) there are no control characters in file names
465+
isInvalidFileName = isCurrentInvalidFileName = true;
466+
inValidNamefileNames = currFileName + "," + inValidNamefileNames;
467+
} else {
468468
let isMatch = false;
469469
let extension = currFileName.split('.').pop().toLowerCase();
470470
let mimeType = file.type || self.extensionToMimeTypeMap[extension];
471471

472472
// If no MIME type is detected, check if the file extension is in the accept list
473473
if (!mimeType) {
474-
isMatch = this.options.accept.some(function(acceptPattern) {
474+
isMatch = this.options.acceptExtensions.some(function(acceptPattern) {
475475
// Remove leading dot if present and convert to lowercase
476476
let cleanPattern = acceptPattern.replace(/^\./, '').toLowerCase();
477477
return cleanPattern === extension;

ui.tests/test-module/specs/fileinput/fileinput.authoring.cy.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('Page - Authoring', function () {
6060
"File"
6161
);
6262
cy.contains("Validation").should("exist");
63-
63+
6464
// Checking some dynamic behaviours
6565
cy.get("[name='./multiSelection'][type=\"checkbox\"]").should("exist").check();
6666
cy.get(".cmp-adaptiveform-fileinput__minimumFiles").invoke('css', 'display').should('equal','block');
@@ -70,6 +70,19 @@ describe('Page - Authoring', function () {
7070
cy.deleteComponentByPath(fileInputDrop);
7171
}
7272

73+
const testFileInputValidationBehaviour = function(fileInputEditPathSelector, fileInputDrop, isSites) {
74+
if (isSites) {
75+
dropFileInputInSites();
76+
} else {
77+
dropFileInputInContainer();
78+
}
79+
cy.openEditableToolbar(sitesSelectors.overlays.overlay.component + fileInputEditPathSelector);
80+
cy.invokeEditableAction("[data-action='CONFIGURE']");
81+
cy.contains("Validation").should("exist");
82+
cy.get('._coral-Tabs-itemLabel').contains('Validation').click();
83+
cy.contains("Allowed file extensions").should("exist");
84+
}
85+
7386
context('Open Forms Editor', function() {
7487
const pagePath = "/content/forms/af/core-components-it/blank",
7588
fileInputEditPath = pagePath + afConstants.FORM_EDITOR_FORM_CONTAINER_SUFFIX + "/fileinput",
@@ -110,5 +123,9 @@ describe('Page - Authoring', function () {
110123
testFileInputBehaviour(fileInputEditPathSelector, fileInputDrop, true);
111124
});
112125

126+
it('open edit dialog of aem forms FileInput and check validation', function() {
127+
testFileInputValidationBehaviour(fileInputEditPathSelector, fileInputDrop, true);
128+
});
129+
113130
});
114131
});

0 commit comments

Comments
 (0)