Oct 5, 2012

How magento widget form container instantiates a form

In admin for Magento, widget forms are used in order to edit entities. You need a Mage_Adminhtml_Block_Widget_Form_Container that contains a Mage_Adminhtml_Block_Widget_Form (with tabs etc..). The full explanation of how the full edit works is beyond this blog post, and I am concentrating on an issue that I came on.

So, while creating a form, I got the following error:
Fatal error: Call to a member function setData() on a non-object in app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php on line 129 

It took me some time to figure out what going on, and after some digging in mage widget forms, I understood the error. In fact, Magento is not finding the form associated to the form container.

Basically for container to locate the form, it needs 3 attributes, an example below:

    protected $_blockGroup = 'training_commenteav';
    protected $_controller = 'adminhtml_eav_comment';
    protected $_mode = 'edit';

These specify the location of the form. In the parent class, here what's happening:
$this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form')

So for my example it would be "training_commenteav/adminhtml_eav_comment_edit_form" that it will use to create a block instance.

I don't know why you can't just specify the uri of the form as I would have expected. Instead it uses blockgroup, controller and mode. Maybe you could have an abstract class for several modes and reuse the blockgroup and controller which I don't think is that useful. Anyway that is how Magento works with widget forms.


13 comments:

  1. You took a whole lot of trouble providing us with some answers for these types of problem. I give you my appreciation for that. Looking forward for more solutions for magento-related problems.

    ReplyDelete
  2. Relally great information..thx for sharing

    ReplyDelete
  3. Thanks,
    I've been watching at this error for two days ;) Solved.

    ReplyDelete
  4. Very useful information, you've inspired to look into the core more, thanks!

    ReplyDelete
  5. Thank you very much! I was just to start hitting the wall with my head :)

    ReplyDelete
  6. You are amazing. Now I always check magento's classes. Thanks

    ReplyDelete
  7. Thanks a lot!! It really help me solve my form trouble.

    ReplyDelete
  8. Thanks man, saved my life in work ;)

    ReplyDelete
  9. Thanks for your post. It's not clear from the first look. I couldn't understood how magento get block path

    ReplyDelete
  10. Wow! This could be one particular of the most beneficial blogs We’ve ever arrive across on this subject. Basically Wonderful. I am also a specialist in this topic therefore I can understand your effort.

    Magento certified developers Adelaide

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. I have installed quick start third party theme. After installation i am getting following error. In localhost it is working fine but when i upload to the server i am facing the error.

    Fatal error: Call to a member function setData() on a non-object in /app/code/local/EM/Ajaxblock/controllers/IndexController.php on line 15

    This is IndexController.php

    getRequest()->getParam('data');
    $data = base64_decode($data);
    $data = json_decode($data);
    $result = array();
    foreach($data as $key => $value)
    {
    $result[$key] = $value;
    }
    //print_r($result);die;
    $block = $this->getLayout()->createBlock($result['type']);
    $block->setData($result);
    if($result['type'] == 'tabs/group')
    {
    $block->setTemplate('emtabs/group.phtml');
    }
    echo $block->toHtml();
    die;
    }
    }
    Please Help.. Thanks in advance..

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete