2

This question already has an answer here:

For backend part of my extension, I am trying to make one file required. The following code is not working. It is working fine for other text fields.

  $fieldset->addField('image1', 'image', array(
      'label'     => Mage::helper('myextension')->__('Image1'),
      'required'  => true,
      'class'     => 'required-file',
      'name'      => 'image1'
  ));

I can see * in the label, but there is no css class in the input field except "input-file". I can still save new entries/posts without uploading an image.

marked as duplicate by Renon Stewart, MeenakshiSundaram R, brentwpeterson, AreDubya, Keyul Shah Sep 26 '15 at 3:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Take a look at Validation does not work in admin forms

What you are trying to accomplish does not seem possible using the 'image' type.

If you take a look at /lib/Varien/Data/Form/Element/Text.php you will notice that in order to add user defined classname you need to call this->addClass('input-text'); (see /lib/Varien/Data/Form/Element/Abstract.php) which is not called in /lib/Varien/Data/Form/Element/Image.php

To accomplish what you trying :

1) Create your own type that extend image

Eg

class MagePal_MyImage_Lib_Varien_Data_Form_Element_Customimage extends Varien_Data_Form_Element_Image
{


}

Then in your form

 $fieldset->addType('customtype', 'MagePal_MyImage_Lib_Varien_Data_Form_Element_Customimage');

 $fieldset->addField('field_id', 'customtype', array(
 'label' => $helper->__('Field label'),
 'name' => 'field_name'
 ));

I think the issue may be in /lib/Varien/Data/Form/Element/Image.php getElementHtml() see $this->setClass('input-file');

See Magento admin form fieldset custom type

2) Use javascript to add the class

$fieldsetImage->addField('logo_big', 'image', array(
        'label'     => Mage::helper('brands')->__('Logo'),
        'title'     => Mage::helper('brands')->__('Logo'),
        'class'     => 'required-entry required-file',
        'required'  => true,
        'name'      => 'logo_big',
        'value'     => $brand->getLogoBig()
    ))->setAfterElementHtml("<script type=\"text/javascript\">$('logo_big').addClassName('required-entry');</script>");
  • Another extension did the same thing in same way you mentioned here. I think this is the best way. – codelogn Sep 28 '15 at 16:03

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