5

I followed the advices of https://magento.stackexchange.com/a/8785/9489 and succeed to get that :

        $fieldsetImage->addField('logo_big', 'image', array(
            'label'     => Mage::helper('brands')->__('Logo'),
            'title'     => Mage::helper('brands')->__('Logo'),
            'class'     => 'required-entry required-file',
            'required'  => Mage::getStoreConfig('clzbrands/general/logo'),
            'name'      => 'logo_big',
            'value'     => $brand->getLogoBig()
        ));

Unfortunately, the html is not generated as expected and I get that :

<tr>
  <td class="label">
   <label for="logo_big">
    Logo<span class="required">*</span>
   </label>
  </td>
  <td class="value">
    <input id="logo_big" class="input-file" type="file" title="Logo" value="" name="logo_big">
  </td>
</tr>

The display

I use a magento 1.9.2 ce without any other module than the one i develop.

The display is correct but I would love to have this validation work. Can anyone help me please ?

  • try by set required to true directly – Piyush Jul 21 '15 at 12:33
  • I tried. The display is good, the field is marked as required but in the facts, I can leave it empty and raise an exception on server-side validation. – Christophe Ferreboeuf Jul 21 '15 at 13:04
3

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>");;
  • 1
    Your help was great as it helped me to find out that it exists a type called imagefile and that fits exactly what I need. Therefore, I do not understand the need of the type image but I will check later. – Christophe Ferreboeuf Jul 22 '15 at 8:00
  • "Image type" will show you a preview of the current image uploaded and also allow you to delete the image. – Renon Stewart Jul 22 '15 at 13:41

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.