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
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();
}
}
Best Answer
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.
You have
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
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.