2

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.

asked Sep 25, 2015 at 17:40
0

1 Answer 1

3

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>");
answered Sep 25, 2015 at 18:09
2
  • Another extension did the same thing in same way you mentioned here. I think this is the best way. Commented Sep 28, 2015 at 16:03
  • You can use the following composer patch to make it work without custom fields in Magento 2: github.com/magento/magento2/issues/… Commented May 16, 2024 at 8:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.