3

I'd created a module for admin where I need to display a form. But its not appearing properly and the form is not submitting.
screenshot
screenshot

directory structure
directory structure

The code is as below

Excellence/PosImg/controllers/Adminhtml/IndexController

class Excellence_PosImg_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->_setActiveMenu('catalog/posimg')
        ;
        $this->_addContent($this->getLayout()->createBlock('posimg/adminhtml_posImg_edit'))
                ->_addLeft($this->getLayout()->createBlock('posimg/adminhtml_posImg_edit_tabs'));
        $this->renderLayout();
    }
}

Excellence/PosImg/Block/Adminhtml/PosImg/Edit

class Excellence_PosImg_Block_Adminhtml_PosImg_Edit extends Mage_Adminhtml_Block_Widget_Form_Container {

    public function __construct() {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_blockGroup = 'posImg';
        $this->_controller = 'adminhtml_index';

        $this->_updateButton('save', 'label', Mage::helper('posimg')->__('Export Data'));

        $this->_formScripts[] = "
            function toggleEditor() {
                if (tinyMCE.getInstanceById('posimg_content') == null) {
                    tinyMCE.execCommand('mceAddControl', false, 'posimg_content');
                } else {
                    tinyMCE.execCommand('mceRemoveControl', false, 'posimg_content');
                }
            }

            function saveAndContinueEdit(){
                editForm.submit($('edit_form').action+'back/edit/');
            }
        ";
    }

    public function getHeaderText() {
        return Mage::helper('posimg')->__('Export POS Images');
    }

}

Excellence/PosImg/Block/Adminhtml/PosImg/Edit/Tabs

class Excellence_PosImg_Block_Adminhtml_PosImg_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs {

    public function __construct() {
        parent::__construct();
        $this->setId('posImg_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('posimg')->__('Item Information'));
    }

    protected function _beforeToHtml() {
        $this->addTab('form_section', array(
            'label' => Mage::helper('posimg')->__('Item Information'),
            'title' => Mage::helper('posimg')->__('Item Information'),
            'content' => $this->getLayout()->createBlock('posimg/adminhtml_posImg_edit_tab_form')->toHtml(),
        ));

        return parent::_beforeToHtml();
    }

}

Excellence/PosImg/Block/Adminhtml/PosImg/Edit/Form

class Excellence_PosImg_Block_Adminhtml_PosImg_Edit_Form extends Mage_Adminhtml_Block_Widget_Form {

    protected function _prepareForm() {
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data'
                )
        );

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }

}

Excellence/PosImg/Block/Adminhtml/PosImg/Edit/Tab/Form

class Excellence_PosImg_Block_Adminhtml_PosImg_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {

    protected function _prepareForm() {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('posImg_form', array('legend' => Mage::helper('posimg')->__('Export POS Images')));

        $fieldset->addField('skip', 'checkbox', array(
            'label' => Mage::helper('posimg')->__('Skip images already exists?'),
            'name' => 'skip',
            'onclick' => 'this.value = this.checked ? 1 : 0;'
        ));
        return parent::_prepareForm();
    }

}
  • pierrefay.com/magento-admin-gridview-85 i'd suggest u to refer this tutorial to create a form/grid in admin panel.. it worked fine for me! – Swetha Jul 21 '14 at 7:02
  • @Swetha: thanks for the link. it may help the future visitors. But it doesn't help me in solving my problem. If following the guide in the link I need to change my module name. – Mohammad Faisal Jul 22 '14 at 13:22
  • Did u get any solution? I have same problem. – Dolly Apr 23 '15 at 5:32
  • yeah... I do not have the complete answer to this problem but the issue was with the $this->_blockGroup = 'posImg'; in Excellence/PosImg/Block/Adminhtml/PosImg/Edit. As in app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php's _prepareLayout() it uses this value to create the form. – Mohammad Faisal Apr 23 '15 at 6:03
  • In my case, I forget to create Form.php file.. – Dolly Apr 23 '15 at 6:18
3

The short answer is $this->_blockgroup and $this->_controller are incorrect, here's an explanation. If we look at line 82 of

app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php

You can see it uses the _blockGroup and _controller to load the correct file path.

$this->setChild('form', $this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'));

You have

    $this->_blockGroup = 'posImg';
    $this->_controller = 'adminhtml_index';

Which translates to posImg/adminhtml_index_edit_tab_form you will have got an exception in your log along the lines of exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_posImg.. I'm assuming here because posImg does not exist Magento defaults back to Mage_.

This should be

    $this->_blockGroup = 'excellence_posImg';
    $this->_controller = 'adminhtml_posImg';

Translating to Excellence_posImg/adminhtml_posImg_edit_tab_form

This is the correct path app/code/local/Excellence/posImg/Block/Adminhtml/posImg/Edit/Tab/Form.php

I resolved the same issue myself by adding print_r($type) on the first line of the try in the createBlock function in app/code/core/Mage/Core/Model/Layout.php

You will see the block names that are being called.

0

So the first thing is when adding your content to the tab your should link the form and not the tab form.

'content' => $this->getLayout()->createBlock('posimg/adminhtml_posImg_edit_form')->toHtml(),

But I do not think that is the only problem with your code. I would suggest having a look at a few tutorials or simply take an example from magento core and work through it.

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.