Magento - edit form in custom module grid

Posted by Shani1351 on Stack Overflow See other posts from Stack Overflow or by Shani1351
Published on 2011-01-10T14:51:55Z Indexed on 2011/01/10 14:53 UTC
Read the original article Hit count: 668

Filed under:
|

I have a custom module and I have a working grid to menage the module items in the admin. My module file structore is : app\code\local\G4R\GroupSales\Block\Adminhtml\Groupsale\
I want to add an edit form so I can view and edit each item in the grid.
I followed this tutorial : http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table#part_2_-_backend_administration
but when the edit page loads, instead of the tab content I get an error :

Fatal error: Call to a member function setData() on a non-object in C:\xampp\htdocs\mystore\app\code\core\Mage\Adminhtml\Block\Widget\Form\Container.php on line 129 

This is my code : /app/code/local/G4R/GroupSales/Block/Adminhtml/Groupsale/Edit.php

 <?php

class G4R_GroupSales_Block_Adminhtml_Groupsale_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_blockGroup = 'groupsale';
        $this->_controller = 'adminhtml_groupsales';

        $this->_updateButton('save', 'label', Mage::helper('groupsales')->__('Save Item'));
        $this->_updateButton('delete', 'label', Mage::helper('groupsales')->__('Delete Item'));
    }

    public function getHeaderText()
    {
        if( Mage::registry('groupsale_data') && Mage::registry('groupsale_data')->getId() ) {
            return Mage::helper('groupsales')->__("Edit Item '%s'", $this->htmlEscape(Mage::registry('groupsale_data')->getTitle()));
        } else {
            return Mage::helper('groupsales')->__('Add Item');
        }
    }
}

/app/code/local/G4R/GroupSales/Block/Adminhtml/Groupsale/Edit/Form.php :

<?php

class G4R_GroupSales_Block_Adminhtml_Groupsale_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',
                                     )
        );

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

/app/code/local/G4R/GroupSales/Block/Adminhtml/Groupsale/Edit/Tabs.php:

<?php

class G4R_GroupSales_Block_Adminhtml_Groupsale_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('groupsales_groupsale_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('groupsales')->__('Groupsale Information'));
    }

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

        return parent::_beforeToHtml();
    }
}

/app/code/local/G4R/GroupSales/Block/Adminhtml/Groupsale/Edit/Tab/Form.php :

<?php

class G4R_GroupSales_Block_Adminhtml_Groupsale_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('groupsales_form', array('legend'=>Mage::helper('groupsales')->__('Item information 3')));

//        $fieldset->addField('title', 'text', array(
//            'label'     => Mage::helper('groupsales')->__('Title'),
//            'class'     => 'required-entry',
//            'required'  => true,
//            'name'      => 'title',
//        ));
//

        if ( Mage::getSingleton('adminhtml/session')->getGroupsaleData() )
        {
            $form->setValues(Mage::getSingleton('adminhtml/session')->getGroupsaleData());
            Mage::getSingleton('adminhtml/session')->setGroupsaleData(null);
        } elseif ( Mage::registry('groupsale_data') ) {
            $form->setValues(Mage::registry('groupsale_data')->getData());
        }
        return parent::_prepareForm();
    }
}

/app/code/local/G4R/GroupSales/controllers/Adminhtml/GroupsaleController.php :

<?php

class G4R_GroupSales_Adminhtml_GroupsaleController extends Mage_Adminhtml_Controller_Action
{

    protected function _initAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('groupsale/items')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
        return $this;
    }

    public function indexAction() {
        $this->_initAction();
        $this->_addContent($this->getLayout()->createBlock('groupsales/adminhtml_groupsale'));
        $this->renderLayout();
    }

    public function editAction()
    {
        $groupsaleId     = $this->getRequest()->getParam('id');
        $groupsaleModel  = Mage::getModel('groupsales/groupsale')->load($groupsaleId);

        if ($groupsaleModel->getId() || $groupsaleId == 0) {

            Mage::register('groupsale_data', $groupsaleModel);

            $this->loadLayout();
            $this->_setActiveMenu('groupsale/items');

            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

            $this->_addContent($this->getLayout()->createBlock('groupsales/adminhtml_groupsale_edit'))
                 ->_addLeft($this->getLayout()->createBlock('groupsales/adminhtml_groupsale_edit_tabs'));

            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('groupsales')->__('Item does not exist'));
            $this->_redirect('*/*/');
        }
    }

    public function newAction()
    {
        $this->_forward('edit');
    }

    public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            try {
                $postData = $this->getRequest()->getPost();
                $groupsaleModel = Mage::getModel('groupsales/groupsale');

                $groupsaleModel->setId($this->getRequest()->getParam('id'))
                    ->setTitle($postData['title'])
                    ->setContent($postData['content'])
                    ->setStatus($postData['status'])
                    ->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setGroupsaleData(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setGroupsaleData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction()
    {
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $groupsaleModel = Mage::getModel('groupsales/groupsale');

                $groupsaleModel->setId($this->getRequest()->getParam('id'))
                    ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }
    /**
     * Product grid for AJAX request.
     * Sort and filter result for example.
     */
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
               $this->getLayout()->createBlock('importedit/adminhtml_groupsales_grid')->toHtml()
        );
    }
}

Any ideas what is the cause for the error?

© Stack Overflow or respective owner

Related posts about php

Related posts about magento