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.


16 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
  14. For instance, Magento's eCommerce Shopping Cart is a platform, which was specifically designed to pander to businesses with owners, who rather sell their products and/or services online.https://medijo.lt/skrajutes/

    ReplyDelete
  15. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best! Magento webshop laten bouwen

    ReplyDelete
  16. Caesars Slots does not require cost to entry and play, but it additionally permits you to purchase virtual objects with actual money inside the sport. You can disable in-app purchases in your device’s settings. 아벤카지노 You could require an internet connection to play Caesars Slots and entry its social options. You also can discover more information about the performance, compatibility and interoperability of Caesars Slots within the above description.

    ReplyDelete