2

Here, I would like to upload image in admin panel grid form in magento. I create images upload in admin panel grid form. Here I attached my sample image upload coding.

[...]
$fieldset->addField('collar_image', 'image', array(
            'name'      => 'collar_image',
            'label'     => Mage::helper('measurement_setter')->__('collar Image'),
            'title'     => Mage::helper('measurement_setter')->__('collar Image'),
            'required'  => true,
            'disabled'  => $isElementDisabled
        ));
[...]

When i save that image data into MYSQL database it results array.And i get response in that save script it results Array.

public function saveAction()
    {
        $redirectPath   = '*/*';
        $redirectParams = array();

        // check if data sent
        $data = $this->getRequest()->getPost();
        echo "<pre>";
        print_r($data);exit;
        if ($data) {
            $data = $this->_filterPostData($data);
            // init model and set data
            /* @var $model Magentostudy_News_Model_Item */
            $model = Mage::getModel('measurement_setter/setter');
 [...]

How to check image values whether correct or not? And How to get check image values?

1 Answer 1

1

I assume you want to upload the image in a specific folder and when you edit one of your entities that has an image, you should see the image near the input with an option to delete.
If so, here is what you need to do.
You need to create a custom renderer for the image field.

Create the file app/code/local/[Vendor]/[Module]/Block/Adminhtml/[Entity]/Helper/Image.php

<?php
class [Vendor]_[Module]_Block_Adminhtml_[Entity]_Helper_Image extends Varien_Data_Form_Element_Image
{
    protected function _getUrl()
    {
        $url = false;
        if ($this->getValue()) {
            $url = Mage::getBaseUrl('media').'/'.'your_folder'.
                $this->getValue();
        }
        return $url;
    }
}

now you have to make your form use this renderer. In the add/edit form you should have a line like this:

$fieldset = $form->addFieldset('....',array('legend' => ...)));

after this line add this:

$fieldset->addType(
        'image',
        Mage::getConfig()->getBlockClassName('[vendor]_[module]/adminhtml_[entity]_helper_image')
    );

Now you should save the image when uploaded.
In your saveAction add this below $data = $this->getRequest()->getPost();

   $input = 'collar_image'; //the image input name
   $destinationFolder = Mage::getBaseDir('media').DS.'your_folder';
   $imageName = '';
   try {
        if (isset($data[$input]['delete'])) {
            $imageName = '';
        } else {
            $uploader = new Varien_File_Uploader($input);
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(true);
            $uploader->setAllowCreateFolders(true);
            $result = $uploader->save($destinationFolder);
            $imageName =  $result['file'];
        }
    } catch (Exception $e) {
        if ($e->getCode() != Varien_File_Uploader::TMP_NAME_EMPTY) {
            throw $e;
        } else {
            if (isset($data[$input]['value'])) {
                $imageName = $data[$input]['value'];
            }
        }
    }
    $data[$input] = $imageName;

Then continue the normal saving process.

Replace in the code above the values between [] with your real values.

  • [Vendor] => your module namespace
  • [Module] => your module name
  • [Entity] => your entity name
2
  • Hi @Marius..i create sample image upload in custom form but need to add more than one image upload in custom form it wont working ? when i add it will simply display image upload tag but doesnt working fine..kindly refer my another question magento.stackexchange.com/questions/54856/…?
    – VIVEK-MDU
    Commented Feb 7, 2015 at 7:19
  • [Entity] means how to go folder. class Magebuzz_Banners_Adminhtml_Banners_Helper_Image extends Varien_Data_Form_Element_Image Commented Feb 12, 2016 at 13:19

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.