Magento 1.9 – Fixing Fatal Error in Edit Block Creation

blockseditmagento-1.9module

Trying to create a custom module to upload .xls file to the magento database. This is the error I get after creating the Edit block for the dashboard:

Fatal error: Call to a member function setData() on boolean in /opt/lampp/htdocs/magento/app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php on line 141

This is my Mymodule/Wine/Block/Adminhtml/Wine/Edit.php

<?php

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

        $this->_objectId     = 'id';
        $this->_blockGroup = 'wine';
        $this->_controller    = 'adminhtml_wine';

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

    public function getHeaderText()
    {
        if (Mage::registry('wine_data') && Mage::registry('wine_data')->getId()) {
            return Mage::helper('wine')->__('Edit Item \'%s\'', $this->htmlEscape(Mage::registry('wine_data')->getTitle()));
        } else {
            return Mage::helper('wine')->__('Upload .xls file');
        }
    }
}

And this is the part of config.xml which corresponds to blocks:

<blocks>
    <wine>
        <class>Mymodule_Wine_Block</class>
    </wine>
</blocks>

Best Answer

I think you are missing the file Mymodule/Wine/Block/Adminhtml/Wine/Edit/Form.php
This should be its content:

<?php 
class Mymodule_Wine_Block_Adminhtml_Wine_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' //add this only if you have file uploads
            )
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}