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

Edit.php

    <?php
class Magecom_Magecomslider_Block_Adminhtml_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_controller = 'adminhtml_magecomslider';
        $this->_mode = 'edit';

        $this->_updateButton('save', 'label', Mage::helper('magecom_magecomslider')->__('Save Image'));
        $this->_updateButton('save', 'id', 'save_button');
        $this->_updateButton('delete', 'label', Mage::helper('magecom_magecomslider')->__('Delete Image'));

        if( $this->getRequest()->getParam($this->_objectId) ) {
            $magecomSlider = Mage::getModel('magecom_magecomslider/magecomslider')
                ->load($this->getRequest()->getParam($this->_objectId));
            Mage::register('magecomSlider', $magecomSlider);
        }
    }

    public function getHeaderText()
    {
        return Mage::helper('magecom_magecomslider')->__('Add Image');
    }
}

Form.php

    <?
class Mage_Adminhtml_Block_Magecom_Magecomslider_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {exit("asd");exit("asd");
        $magecomSlider = Mage::registry('magecomSlider');
        //$product = Mage::getModel('catalog/product')->load($review->getEntityPkValue());
        //$customer = Mage::getModel('customer/customer')->load($review->getCustomerId());
        $magecomslider = Mage::getModel('magecom_magecomslider/magecomslider')->load($this->getRequest()->getParam('id'));

        $form = new Varien_Data_Form(array(
            'id'        => 'edit_form',
            'action'    => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'), 'ret' => Mage::registry('ret'))),
            'method'    => 'post'
        ));

        $fieldset = $form->addFieldset('magecomslider', array('legend' => Mage::helper('magecom_magecomslider')->__('Add Image'), 'class' => 'fieldset-wide'));

        $fieldset->addField('magecomslider', 'image', array(
            'label'     => Mage::helper('magecom_magecomslider')->__('Image'),
            'text'      => 'magecomslider',
        ));

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

MagecomsliderController.php

<?php
class Magecom_Magecomslider_Adminhtml_MagecomsliderController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Magecom Slider'));

        $this->loadLayout();
        $this->_setActiveMenu('magecom_magecomslider');
        $this->_addBreadcrumb(Mage::helper('magecom_magecomslider')->__('Magecom Slider'), Mage::helper('magecom_magecomslider')->__('Magecom Slider'));
        $this->renderLayout();

    }
    public function massAddAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select products.'));
        } else {
            if (!empty($productIds)) {
                try {
                    $countItems = count($productIds);
                    if($countItems>2 || $countItems<2)
                    {
                        $this->_getSession()->addError($this->__('You should select only 2 products.'));
                    }else{
                        $slider_items = Mage::getModel('magecom_magecomslider/magecomslider');
                        $collection_slider_items = $slider_items->getCollection();
                        $collection_slider_items_count = $collection_slider_items->count();
                        if($collection_slider_items_count==2)
                        {
                            $this->_getSession()->addError($this->__('Sliders exists.'));
                        }else{
                            foreach ($productIds as $productId) {
                                $data = array('product_id' => $productId);
                                $model = Mage::getModel('magecom_magecomslider/magecomslider');
                                $model->setData($data);
                                $model->save();
                            }
                            $this->_getSession()->addSuccess(
                                $this->__('Products added to the slideshow.')
                            );
                        }    
                    }
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/index');
    }
    public function gridAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
    public function deleteAction()
    {
        try {
            $slider_items = Mage::getModel('magecom_magecomslider/magecomslider');
            $collection_slider_items = $slider_items->getCollection();
            foreach($collection_slider_items as $item)
            {
                $item->delete();
            }
            $this->_getSession()->addSuccess(
                $this->__('Sliders deleted successfully.')
            );  
        }catch (Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        }
        $this->_redirect('*/*/index');
    }
    public function editAction()
    {
        $this->loadLayout();
        $product_id = $this->getRequest()->getParam('id');

        $slider_items = Mage::getModel('magecom_magecomslider/magecomslider');
        $collection_slider_items = $slider_items->getCollection();
        $collection_slider_items_count = $collection_slider_items->count();
        if($collection_slider_items_count==2)
        {

        }else{
            $this->_getSession()->addError($this->__('Please select products for slider.'));
        }
        $this->renderLayout();
        //exit($product_id);
    }
    public function saveAction()
    {
        exit("Save");
    }
}

And look please in Form.php exit does not work, i think its mean what this form not called, i dont know why.

magecomslider.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <adminhtml_magecomslider_index>
        <reference name="content">
            <block type="magecom_magecomslider/adminhtml_magecomslider" name="magecomslider.grid_container" />
        </reference>
    </adminhtml_magecomslider_index>

    <adminhtml_magecomslider_new>
        <update handle="adminhtml_magecomslider_edit"/>
    </adminhtml_magecomslider_new>

    <adminhtml_magecomslider_grid>
        <block type="core/text_list" name="root" output="toHtml">
            <block type="magecom_magecomslider/adminhtml_magecomslider" name="magecomslider.grid_container" />
        </block>
    </adminhtml_magecomslider_grid>
    <adminhtml_magecomslider_edit>
        <reference name="content">
            <block type="magecom_magecomslider/adminhtml_edit" name="magecomslider.form_container" />
        </reference>
    </adminhtml_magecomslider_edit>
</layout>

Magecomslider.php - model

<?php
class Magecom_Magecomslider_Model_Magecomslider extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('magecom_magecomslider/magecomslider');
    }
}

Magecomslider.php - resourse model

<?php
class Magecom_Magecomslider_Model_Resource_Magecomslider extends Mage_Core_Model_Mysql4_Abstract
{
    protected function _construct()
    {
        $this->_init('magecom_magecomslider/magecomslider', 'id');
    }
}

Collection.php

<?php
class Magecom_Magecomslider_Model_Resource_Magecomslider_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    protected function _construct()
    {
        $this->_init('magecom_magecomslider/magecomslider');
    }
}
9

In your form container constructor you have not declared your blockGroup which will be used to instantiate the form block. Ref Mage_Adminhtml_Block_Widget_Form_Container::_prepareLayout():

protected function _prepareLayout()
{
    if ($this->_blockGroup && $this->_controller && $this->_mode) {
        $this->setChild('form', $this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'));
    }
    return parent::_prepareLayout();
}

You often won't see this in core Adminhtml forms because Magento can cheat with their Mage namespace.

  • I am added it to Magecom_Magecomslider_Block_Adminhtml_Magecomslider_Edit but this does not help. Error exist. Maybe could you look at my code via team viewer? Please help me. I am two days can't solve this problem. – Arthur Yakovlev Apr 26 '14 at 15:26
  • 2
    Look at the _prepareLayout() definition again. Your _controller value is wrong. You want it to build the factory argument for your block: createBlock("{_blockgroup}/{_controller}_{_mode}_form") – benmarks Apr 26 '14 at 15:35
  • $form_block = $this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'; echo $form_block; is magecomslider/adminhtml_magecomslider_edit_form, so its wrong now? And how it do right? I am new in magento – Arthur Yakovlev Apr 26 '14 at 15:38
  • Move your form block from under Mage/Adminhtml! Then you will use _blockGroup = magecom_magecomslider, _controller = adminhtml_magecomslider and _mode = edit. – benmarks Apr 26 '14 at 16:15
  • but now my blocks placed at app\code\local\Magecom\Magecomslider\Block/Adminhtml – Arthur Yakovlev Apr 26 '14 at 16:25

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.