7

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")
            )
        );
1
  • Have you tried with 'required' => true, without ' quotes ?
    – Roney
    Commented Jul 14, 2014 at 6:30

4 Answers 4

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 :(

2
  • Is there any other possiblity to add class required-entry
    – surya
    Commented Jul 28, 2014 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 Commented Jul 28, 2014 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>');
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',
                   )
    );
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")
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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