0

I added some extra fields to store_group admin entry, the issue is that i can't upload a image file because i hanven't the enctype="multipart/form-data" in my form when i inspect the HTML code,and Im sure that the issue comes from this, i tried to add it like this but it doesn't work !

Namespace/Module/Block/Adminhtml/System/Store/Edit/Form.php

protected function _prepareForm(){

    $form = new Varien_Data_Form(array(
        'id'        => 'edit_form',
        'action'    => $this->getData('action'),
        'method'    => 'post',
        'enctype' => 'multipart/form-data' //My add for enctype upload image !important
    ));
    $form->setUseContainer(true);
    $this->setForm($form);
    parent::_prepareForm();

    if (Mage::registry('store_type') == 'group' && Mage::registry('store_action') == "edit")
    {
        $groupModel = Mage::registry('store_data');
        $fieldset = $this->getForm()->getElement('group_fieldset');

        //The extra fields
        ...
        $fieldset->addField('group_storelocator_image_url', 'image', array(
            'name' => 'group[storelocator_image_url]',
            'label' => 'Store Image',
            'required' => false,
            'note' => $groupModel->getStorelocatorImageUrl() != NULL ? "<a href=\"" . Mage::getBaseUrl('media') . $groupModel->getStorelocatorImageUrl() . "\" target=\"_blank\" ><img src=\"" . Mage::getBaseUrl('media') . $groupModel->getStorelocatorImageUrl() . "\" width=\"60\" /><br />" . $groupModel->getStorelocatorImageUrl() . "</a>" : "",
            'disabled' => $groupModel->isReadOnly(),
        ));
    }
    return $this;
}

  Namespace/Module/ModuleController.php

class Namespace_Module_ModuleController extends Mage_Adminhtml_Controller_Action
{
    public function saveAction()
    {
        if ($data = $this->getRequest()->getPost()) {
            if (isset($_FILES['storelocator_image_url']['name']) && $_FILES['storelocator_image_url']['name'] != '') {
                try {
                    $path = Mage::getBaseDir() . DS . 'store' . DS;  //desitnation directory in store folder
                    $fname = $_FILES['storelocator_image_url']['name']; //file name
                    $fullname = $path . $fname;
                    $uploader = new Varien_File_Uploader('storelocator_image_url'); //load class
                    $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); //Allowed extension for file
                    $uploader->setAllowCreateFolders(true); //for creating the directory if not exists
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $uploader->save($path, $fname);
                } catch (Exception $e) {
                    $fileType = "Invalid file format";
                }
            }
        }
        if ($fileType == "Invalid file format") {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('foundation')->__($fname . " Invalid file format"));
            $this->_redirect('*/*/');
            return;
        }
    }
}
  • what if you use return parent::_prepareForm(); instead of return $this in Form.php ? – Piyush Nov 14 '17 at 13:26
  • I tried it and it tells me an error, addField() on null – PЯINCƏ Nov 14 '17 at 13:27
0

In your Form.php Try below code

protected function _prepareForm(){

    $form = new Varien_Data_Form(array(
        'id'        => 'edit_form',
        'action'    => $this->getData('action'),
        'method'    => 'post',
        'enctype' => 'multipart/form-data' //My add for enctype upload image !important
    ));


    if (Mage::registry('store_type') == 'group' && Mage::registry('store_action') == "edit")
    {
        $groupModel = Mage::registry('store_data');
        $fieldset = $this->getForm()->getElement('group_fieldset');

        //The extra fields
        ...
        $fieldset->addField('group_storelocator_image_url', 'image', array(
            'name' => 'group[storelocator_image_url]',
            'label' => 'Store Image',
            'required' => false,
            'note' => $groupModel->getStorelocatorImageUrl() != NULL ? "<a href=\"" . Mage::getBaseUrl('media') . $groupModel->getStorelocatorImageUrl() . "\" target=\"_blank\" ><img src=\"" . Mage::getBaseUrl('media') . $groupModel->getStorelocatorImageUrl() . "\" width=\"60\" /><br />" . $groupModel->getStorelocatorImageUrl() . "</a>" : "",
            'disabled' => $groupModel->isReadOnly(),
        ));
    }
    $form->setUseContainer(true);
    $this->setForm($form);
    return parent::_prepareForm();

}
  • Nop, i have an error in $fieldset = $this->getForm()->getElement('group_fieldset'); i changed $this with $form and i'll have another error : Call to a member function addField() on null in – PЯINCƏ Nov 14 '17 at 14:12

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.