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.