Skip to content

Commit d652a8b

Browse files
pavi41Pavitra Khatri
andauthored
Configured custom validation messages in file attachment (#1459)
Co-authored-by: Pavitra Khatri <pavitrakhatri@pavitras-mbp.corp.adobe.com>
1 parent 8498814 commit d652a8b

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@
8888
maxFileSize="2"
8989
type="file[]"
9090
visible="{Boolean}true"/>
91+
<fileinput6
92+
jcr:primaryType="nt:unstructured"
93+
jcr:title="File Input - 6"
94+
sling:resourceType="core/fd/components/form/fileinput/v3/fileinput"
95+
accept="[audio/*, video/*, image/*, text/*, application/pdf]"
96+
buttonText="Attach Files"
97+
enabled="{Boolean}true"
98+
maxFileSizeMessage="File size is greater than the expected size!"
99+
fieldType="file-input"
100+
name="fileinput6"
101+
readOnly="{Boolean}true"
102+
textIsRich="[true,true]"
103+
maxFileSize="2"
104+
type="file[]"
105+
visible="{Boolean}true"/>
91106
<submit
92107
jcr:lastModified="{Date}2023-01-17T16:28:58.844+05:30"
93108
jcr:lastModifiedBy="admin"

ui.frontend/src/view/FormFileInputWidgetBase.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,16 @@ class FormFileInputWidgetBase {
298298
}
299299
}
300300

301-
invalidMessage(fileName, invalidFeature){
302-
// todo: have add localization here
303-
if(invalidFeature === this.invalidFeature.SIZE) {
304-
alert(FormView.LanguageUtils.getTranslatedString(this.lang, "FileSizeGreater", [fileName, this.options.maxFileSize]));
305-
} else if (invalidFeature === this.invalidFeature.NAME) {
306-
alert(FormView.LanguageUtils.getTranslatedString(this.lang, "FileNameInvalid", [fileName]));
307-
} else if (invalidFeature === this.invalidFeature.MIMETYPE) {
308-
alert(FormView.LanguageUtils.getTranslatedString(this.lang, "FileMimeTypeInvalid", [fileName]));
309-
}
301+
invalidMessage(fileName, invalidFeature) {
302+
const customMessages = this.options.constraintMessages || {};
303+
304+
const messages = {
305+
[this.invalidFeature.SIZE]: customMessages.maxFileSize || FormView.LanguageUtils.getTranslatedString(this.lang, "FileSizeGreater", [fileName, this.options.maxFileSize]),
306+
[this.invalidFeature.NAME]: customMessages.invalidFileName || FormView.LanguageUtils.getTranslatedString(this.lang, "FileNameInvalid", [fileName]),
307+
[this.invalidFeature.MIMETYPE]: customMessages.invalidMimeType || FormView.LanguageUtils.getTranslatedString(this.lang, "FileMimeTypeInvalid", [fileName])
308+
};
309+
310+
alert(messages[invalidFeature]);
310311
}
311312

312313

ui.tests/test-module/specs/fileinput/fileinputv3.runtime.cy.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,16 @@ describe('Click on button tag (V-3)', () => {
236236
});
237237

238238
});
239+
240+
it('should display an error message configured by the user', () => {
241+
const [id, fieldView] = Object.entries(formContainer._fields)[5];
242+
const model = formContainer._model.getElement(id);
243+
244+
const fileInput = 'input[name=\'fileinput6\']';
245+
cy.attachFile(fileInput, ['FileAttachment3mb.jpg']);
246+
247+
cy.on('window:alert', (alertText) => {
248+
expect(alertText).to.equal(model.getState().constraintMessages.maxFileSize);
249+
});
250+
});
239251
})

0 commit comments

Comments
 (0)