Skip to content

Commit d5594fc

Browse files
MAGETWO-95645: Custom file attribute works incorrect
1 parent 4671f49 commit d5594fc

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

app/code/Magento/Eav/Model/Attribute/Data/File.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected function _validateByRules($value)
147147
return $this->_fileValidator->getMessages();
148148
}
149149

150-
if (!is_uploaded_file($value['tmp_name'])) {
150+
if (!empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
151151
return [__('"%1" is not a valid file.', $label)];
152152
}
153153

@@ -174,12 +174,21 @@ public function validateValue($value)
174174
if ($this->getIsAjaxRequest()) {
175175
return true;
176176
}
177+
$fileData = $value;
178+
179+
if (is_string($value) && !empty($value)) {
180+
$dir = $this->_directory->getAbsolutePath($this->getAttribute()->getEntityType()->getEntityTypeCode());
181+
$fileData = [
182+
'size' => filesize($dir . $value),
183+
'name' => $value
184+
];
185+
}
177186

178187
$errors = [];
179188
$attribute = $this->getAttribute();
180189

181190
$toDelete = !empty($value['delete']) ? true : false;
182-
$toUpload = !empty($value['tmp_name']) ? true : false;
191+
$toUpload = !empty($value['tmp_name']) || is_string($value) && !empty($value) ? true : false;
183192

184193
if (!$toUpload && !$toDelete && $this->getEntity()->getData($attribute->getAttributeCode())) {
185194
return true;
@@ -195,11 +204,13 @@ public function validateValue($value)
195204
}
196205

197206
if ($toUpload) {
198-
$errors = array_merge($errors, $this->_validateByRules($value));
207+
$errors = array_merge($errors, $this->_validateByRules($fileData));
199208
}
200209

201210
if (count($errors) == 0) {
202211
return true;
212+
} elseif (is_string($value) && !empty($value)) {
213+
$this->_directory->delete($dir . $value);
203214
}
204215

205216
return $errors;

dev/tests/js/jasmine/tests/lib/mage/validation.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,4 +1142,24 @@ define([
11421142
)).toEqual(true);
11431143
});
11441144
});
1145+
1146+
describe('Testing validate-forbidden-extensions', function () {
1147+
it('validate-forbidden-extensions', function () {
1148+
var el1 = $('<input type="text" value="" ' +
1149+
'class="validate-extensions" data-validation-params="php,phtml">').get(0);
1150+
1151+
expect($.validator.methods['validate-forbidden-extensions']
1152+
.call($.validator.prototype, 'php', el1, null)).toEqual(false);
1153+
expect($.validator.methods['validate-forbidden-extensions']
1154+
.call($.validator.prototype, 'php,phtml', el1, null)).toEqual(false);
1155+
expect($.validator.methods['validate-forbidden-extensions']
1156+
.call($.validator.prototype, 'html', el1, null)).toEqual(true);
1157+
expect($.validator.methods['validate-forbidden-extensions']
1158+
.call($.validator.prototype, 'html,png', el1, null)).toEqual(true);
1159+
expect($.validator.methods['validate-forbidden-extensions']
1160+
.call($.validator.prototype, 'php,html', el1, null)).toEqual(false);
1161+
expect($.validator.methods['validate-forbidden-extensions']
1162+
.call($.validator.prototype, 'html,php', el1, null)).toEqual(false);
1163+
});
1164+
});
11451165
});

lib/internal/Magento/Framework/Data/Form/Element/Text.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function getHtmlAttributes()
6565
'placeholder',
6666
'data-form-part',
6767
'data-role',
68+
'data-validation-params',
6869
'data-action'
6970
];
7071
}

lib/web/mage/validation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,27 @@
881881
},
882882
$.mage.__('Please enter a valid number in this field.')
883883
],
884+
'validate-forbidden-extensions': [
885+
function (v, elem) {
886+
var forbiddenExtensions = $(elem).attr('data-validation-params'),
887+
forbiddenExtensionsArray = forbiddenExtensions.split(','),
888+
extensionsArray = v.split(','),
889+
result = true;
890+
891+
this.validateExtensionsMessage = $.mage.__('Forbidden extensions has been used. Avoid usage of ') +
892+
forbiddenExtensions;
893+
894+
$.each(extensionsArray, function (key, extension) {
895+
if (forbiddenExtensionsArray.indexOf(extension) !== -1) {
896+
result = false;
897+
}
898+
});
899+
900+
return result;
901+
}, function () {
902+
return this.validateExtensionsMessage;
903+
}
904+
],
884905
'validate-digits-range': [
885906
function (v, elm, param) {
886907
var numValue, dataAttrRange, classNameRange, result, range, m, classes, ii;

0 commit comments

Comments
 (0)