3

I have add an image upload field in my modules edit form using-

$fieldset->addField(
    'image',
    'image',
    [
        'name' => 'image',
        'label' => __('Image'),
        'id' => 'image',
        'title' => __('Image'),
        'class' => 'required-entry',
        'required' => true,
    ]
);

While editing row it displays checkbox to delete image, I have only one image upload field and that is required also. So I want to remove this checkbox. How can I remove this checkbox?

1

1 Answer 1

0

As @Manashvi Suggested I tried to solve it the way it was done in Magento 1.x and it worked. I created a custom renderer for the image. and created a class in my module [Namespace]_[Module]_Block_Adminhtml_Helper_Image_Required with this content

class Required extends \Magento\Framework\Data\Form\Element\Image
{
    protected function _getDeleteCheckbox()
    {
        return '';
    }
}

Then in my form block, right above field added this lines

$fieldset->addType('required_image', 'Namespace\Module\Block\Adminhtml\Helper\Image\Required');

and defined field like this:

$fieldset->addField(
    'image',
    'required_image',               [
    'name' => 'image',
    'label' => __('Image'),
    'id' => 'image',
    'title' => __('Image'),
    'class' => 'required-entry',
    'required' => true,
    ]
);
4
  • I am trying to achieve the same. but i get error: Cannot declare class Required because the name is already in use. Even i change it's name to something else, still i get same error with that name. Any idea?
    – Khushbu
    Feb 28, 2019 at 9:58
  • such issue occurs in case when you have declared same class more than once, please check your file path and class name
    – Piyush
    Feb 28, 2019 at 9:59
  • but even if i change class name to any other name for e.g. RequiredImage, Still i get the same error: Cannot declare class RequiredImage because the name is already in use. (i am using magento 2.2)
    – Khushbu
    Feb 28, 2019 at 10:03
  • Ok i found the issue. Just forgot to add namespace line on top of the class file!
    – Khushbu
    Feb 28, 2019 at 10:09

Your Answer

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

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