I have a custom module which manage to upload the banners.

Everything working fine except image validation.

Below is my code for the image in


namespace E25media\Bannermanager\Block\Adminhtml\Banners\Edit\Tab;

class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
   /* -- some code here */ 
protected function _prepareForm()
     /* -- some code here */ 
     $fieldset->addType('image', '\E25media\Bannermanager\Block\Adminhtml\Banners\Helper\Image');

$fieldset->addField('banner_image_mobile', 'image', array(
'name'      => 'banner_image_mobile',
'class'     => 'required-entry required-file',
'label' => __('Mobile Banner Image'),
'title' => __('Mobile Banner Image'),
'required'  => true,
'disabled'  => $isElementDisabled));

  /* -- some code here */ 

My form file upload is look like this. enter image description here

But i can submit the form without uploading the image.

How to validate the image file ?

1 Answer 1


Try this.
Use javascript to add the class.

        'name' => 'banner_image_mobile', 
        'label' => __('Mobile Banner Image'), 
        'title' => __('Mobile Banner Image'),             
        'note' => __('Size: 164px x 24px, Allowed File Types : (JPG,JPEG,PNG), Max Size:2MB.'), 
        'path'=> $model->getImagePath(),
        'required' => true, 
        'value' => $model->getImagePath()
            ], function($){
                $(document).ready(function () {
                    $( "#page_banner_image_mobile" ).attr( "accept", "image/x-png,image/gif,image/jpeg,image/jpg,image/png" );
  • thanks for your answer.I am getting this error Parse error: syntax error, unexpected ''disabled'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in D:\xampp\htdocs\tork\app\code\E25media\Bannermanager\Block\Adminhtml\Banners\Edit\Tab\Main.php on line 159
    – Mujahidh
    May 9, 2018 at 7:06
  • check my updated answer and compare with your current code. May 9, 2018 at 7:20
  • @Mujahidh still getting error? May 9, 2018 at 8:40
  • still allow me to submit the form without upload the image
    – Mujahidh
    May 9, 2018 at 9:59
  • I think there is a problem with your field id mentioned in script, alert in JavaScript code and check field id is correct or not, because this code working for me. May 9, 2018 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.