6

How to make the add field for image in custom module admin grid as required entry?

The following is the way i have given , but it is not taking as required - entry field while saving.

$fieldset->addField(
            'background_image', 
            'image',
            array(
                'label' => $this->__("Background Image"),
                'class' => 'required-entry',
                'required' => 'true',
                'name' => 'background_image',
                'note' => $this->__("Background Image")
            )
        );

        $fieldset->addField(
            'background_image_hover', 
            'image',
            array(
                'label' => $this->__("Background Hover Image"),
                'class' => 'required-entry',
                'required' => 'true',
                'name' => 'background_image_hover',
                'note' => $this->__("Background Hover Image")
            )
        );
  • Have you tried with 'required' => true, without ' quotes ? – Roney Jul 14 '14 at 6:30
4

The problem happens because the image field type uses Varien_Data_Form_Element_Image which in the function getElementHtml calls setClass this un-setting any previously set classes for example the required-entry class :(

What is should do is call $this->addClass('input-file'); so I guess this is a bug in Magento :(

| improve this answer | |
  • Is there any other possiblity to add class required-entry – surya Jul 28 '14 at 13:00
  • 1
    The only other thing is to specify your own form element mapping and so you do not have to update the core Varien_Data_Form_Element_Image file. magento.stackexchange.com/a/30203/158 – David Manners Jul 28 '14 at 13:02
0

You can try this code after addField :

$field1->setAfterElementHtml('<script>
//< ![C
$("image").addClassName("required-entry required-file ");
// here goes your custom Javascript

//]]>
</script>');
| improve this answer | |
0

Use type file instead image like below

$fieldSet->addField(
            'image', 'file', array(
                   'label'    => $this->__("Background Hover Image"),
                   'class'    => 'required-entry',
                   'required' => true,
                   'name'     => 'image',
                   )
    );
| improve this answer | |
0

Change field type to 'imagefile':

$fieldset->addField(
    'background_image', 
    'imagefile',
    array(
        'label' => $this->__("Background Image"),
        'class' => 'required-entry',
        'required' => 'true',
        'name' => 'background_image',
        'note' => $this->__("Background Image")
    )
);
| improve this answer | |

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.