I'm new to Magento 2.3, i want to validate file upload field.
- Allowed file formats: jpeg.
 
I put a method in lib\web\mage\validation.js
require([
 'jquery',
 'jquery/ui',
 'jquery/validate',
 'mage/translate'
 ], function ($) {
 //Validate Image Extensions
 $.validator.addMethod(
 'validate-fileextensions', function (v, elm) {
 var extensions = ['jpeg'];
 if (!v) {
 return true;
 }
 with (elm) {
 var ext = value.substring(value.lastIndexOf('.') + 1);
 for (i = 0; i < extensions.length; i++) {
 if (ext == extensions[i]) {
 return true;
 }
 }
 }
 return false;
 }, $.mage.__('Disallowed file type.'));
});
and add this class validate-fileextensions in file> input, in this file app\code\{vendor_name}\{module_name}\Block\Adminhtml\Form\Edit\Tab\Main.php
 $fieldset->addField(
 'filename',
 'image',
 [
 'label' => __('File'),
 'title' => __('File'),
 'name' => 'filename',
 'required' => true,
 'disabled' => $isElementDisabled,
 'class' => 'validate-fileextensions'
 ]
 );
but its not working for me, also when i inspect on file> input, i can't see my custom class their. see below image. enter image description here
2 Answers 2
You can put the validation into your controller, which save the value of this field like this.
public function __construct(
 ...
 \Magento\Framework\File\UploaderFactory $uploaderFactory,
 ...
) {
 ......
 $this->uploaderFactory = $uploaderFactory;
 .....
}
public function execute()
{
 try {
 $destinationFolder = 'PATH OF FOLDER WHERE YOU NEED TO UPLOAD FILE';
 $yourInputFileName = 'YOURFILEUPLOADFIELDNAME';
 $uploader = $this->uploaderFactory->create(array('fileId' => $yourInputFileName));
 $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
 $uploader->setAllowRenameFiles(true);
 $uploader->setFilesDispersion(true);
 $uploader->setAllowCreateFolders(true);
 $result = $uploader->save($destinationFolder);
 return $result['file'];
 } catch (\Exception $e) { 
 throw new FrameworkException($e->getMessage());
 }
}
NOTE $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']); in this line you can specify which file type you need to allowed.
- 
 i have
app\code\{vendor_name}\{module_name}\Controller\Adminhtml\Form\Index\Save.php, should i put this code in that file ... ? @Dhiren VasoyaPartab Saifuddin Zakir– Partab Saifuddin Zakir2019年05月07日 10:47:32 +00:00Commented May 7, 2019 at 10:47 - 
 Yes, you can validate file there and show error message according to that.Dhiren Vasoya– Dhiren Vasoya2019年05月07日 10:49:26 +00:00Commented May 7, 2019 at 10:49
 - 
 Ok let me try :)Partab Saifuddin Zakir– Partab Saifuddin Zakir2019年05月07日 10:50:45 +00:00Commented May 7, 2019 at 10:50
 - 
 I already write the comment that, this is the path of folder where you upload the files into magento.Dhiren Vasoya– Dhiren Vasoya2019年05月07日 11:01:11 +00:00Commented May 7, 2019 at 11:01
 - 
 After adding this line
\Magento\Framework\File\UploaderFactory $uploaderFactory,i get an error something like this...1 exception(s): Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: EC\Customimport\Controller\Adminhtml\Index\Save\Interceptor etc...@Dhiren VasoyaPartab Saifuddin Zakir– Partab Saifuddin Zakir2019年05月07日 11:23:36 +00:00Commented May 7, 2019 at 11:23 
Try This :-
$fieldset->addField(
 'filename',
 'file',
 [
 'label' => __('File'),
 'title' => __('File'),
 'name' => 'filename',
 'required' => true,
 'disabled' => $isElementDisabled
 ]
 );
 - 
 Bro with you code user can still upload other extention file... @RkRathodPartab Saifuddin Zakir– Partab Saifuddin Zakir2019年05月09日 12:41:20 +00:00Commented May 9, 2019 at 12:41
 - 
 which extension file upload???Ronak Rathod– Ronak Rathod2019年05月09日 12:42:27 +00:00Commented May 9, 2019 at 12:42
 - 
 your code let me upload all file extenton, when i click on
all filewhenfile manageropens....Partab Saifuddin Zakir– Partab Saifuddin Zakir2019年05月09日 12:44:25 +00:00Commented May 9, 2019 at 12:44 - 
 try this code..Ronak Rathod– Ronak Rathod2019年05月09日 12:49:48 +00:00Commented May 9, 2019 at 12:49
 - 
 yeah exactly, before raising this question, i applied
filetag instead of imageimagebut my senior told me to keepimagetag, coz it has a preview feature and checkbox delete feature... In Magento core elements, their is aImage.phpfile which belongs to thisimagetag, and it has a built-in class, i thinkinput-file... I also saw many answers for applying custom class inImagetag but they all were very old question so i couldn't understand it... @RkRathodPartab Saifuddin Zakir– Partab Saifuddin Zakir2019年05月10日 06:47:46 +00:00Commented May 10, 2019 at 6:47 
Explore related questions
See similar questions with these tags.
['jpg','jpeg','gif','png','csv','xlsx']