Validation for file type not working in File Upload in Magento 2 Store Configuration
-
04-03-2021 - |
Вопрос
I am following below Article to upload file in store configuration: https://www.mageplaza.com/devdocs/how-file-upload-sytem-configuration-magento-2.html
I have defined custom backend model as mentioned in this artice to validate my file type:
<?php
namespace Vendor\Package\Model\Config\Backend;
class CustomFileType extends \Magento\Config\Model\Config\Backend\File
{
/**
* @return string[]
*/
public function getAllowedExtensions() {
return ['csv'];
}
}
?>
and in system.xml, it's like this:
<field id="import" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Import CSV</label>
<comment>Comment</comment>
<backend_model>Vendor\Package\Model\Config\Backend\CustomFileType</backend_model>
<upload_dir>upload</upload_dir>
</field>
but it's not working. I can upload image files too!!
What's wrong with this code?
Решение
I found answer by referring
vendor\magento\module-config\Model\Config\Backend\File.php
Correct Function name is _getAllowedExtensions not getAllowedExtensions
So, correct method is:
public function _getAllowedExtensions() {
return ['csv'];
}
Другие советы
Replace backend_model with this :-
<backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
Не связан с magento.stackexchange