Skip to content

Commit 6659aa6

Browse files
committed
#436: Cannot Drag and Drop Image to Upload Area in Banner, Slide in Safari, IE11 - Added new validation method with longer message
1 parent 3a83cc7 commit 6659aa6

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/image-uploader.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ define([
88
'underscore',
99
'uiRegistry',
1010
'Magento_Ui/js/form/element/image-uploader',
11+
'Magento_Ui/js/lib/validation/validator',
1112
'Magento_PageBuilder/js/resource/resize-observer/ResizeObserver',
1213
'Magento_PageBuilder/js/events',
1314
'mage/translate'
14-
], function ($, _, uiRegistry, Uploader, ResizeObserver, events, $t) {
15+
], function ($, _, uiRegistry, Uploader, validator, ResizeObserver, events, $t) {
1516
'use strict';
1617

1718
var initializedOnce = false;
@@ -80,6 +81,16 @@ define([
8081
new ResizeObserver(this.updateResponsiveClasses.bind(this)).observe(this.$uploadArea.get(0));
8182
},
8283

84+
/**
85+
* Checks if extension of provided file is allowed.
86+
* {@inheritDoc}
87+
*/
88+
isExtensionAllowed: function (file) {
89+
var fileExtensions = this.getAllowedFileExtensionsInCommaDelimitedFormat(),
90+
fileExtensionMessage = this.translations.allowedFileTypes + ':\t' + fileExtensions;
91+
return validator('validate-file-extension', file.name, this.allowedExtensions, fileExtensionMessage);
92+
},
93+
8394
/**
8495
* Remove draggable classes from dropzones
8596
* {@inheritDoc}

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/validator-rules-mixin.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ define([
124124
return !!style.width.length;
125125
}
126126

127+
/**
128+
* Validate file extension.
129+
*
130+
* @param {String} name
131+
* @param {String} types
132+
* @returns {Boolean}
133+
*/
134+
function validateFileExtension(name, types) {
135+
var extension = name.split('.').pop().toLowerCase();
136+
137+
if (types && typeof types === 'string') {
138+
types = types.split(' ');
139+
}
140+
141+
return !types || !types.length || ~types.indexOf(extension);
142+
}
143+
127144
return function (validator) {
128145
var requiredInputRule = validator.getRule('required-entry');
129146

@@ -254,6 +271,20 @@ define([
254271
$.mage.__('Please enter a valid number or calculation: Valid numbers must have an extension (px, %, pt, vh). Calculations must have white space around the + and - operators and cannot divide by zero.')//eslint-disable-line max-len
255272
);
256273

274+
validator.addRule(
275+
'validate-file-extension',
276+
function (name, types, additionalMessage) {
277+
this.fileTypeErrorMessage = $.mage.__('We don\'t recognize or support this file extension type.');
278+
if (additionalMessage) {
279+
this.fileTypeErrorMessage += ('\t' + additionalMessage);
280+
}
281+
return validateFileExtension(name, types);
282+
},
283+
function () {
284+
return this.fileTypeErrorMessage;
285+
}
286+
);
287+
257288
validateObjectField(validator, 'validate-number');
258289
validateObjectField(validator, 'less-than-equals-to');
259290
validateObjectField(validator, 'greater-than-equals-to');

0 commit comments

Comments
 (0)