In my custom module, I have a admin grid which I added through extending the Mage class Mage_Adminhtml_Block_Widget_Grid.

Now I have a requirement that the above created grid should be wrapped in a form. After referring core files, I found that I can do this by extending Mage class Mage_Adminhtml_Block_Widget_Form. So, I did the same as follows:

class Namespace_Module_Block_Adminhtml_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'
        return parent::_prepareForm();

After adding the above file, I called this form in __construct of Grid.php of my custom module:

public function __construct()
    /* other code here */
    $this->setDestElementId('edit_form');  //I think this should wrap the grid in form. (not sure)

Still the Admin grid is not being wrapped in a Form.

I am trying to wrap the grid in Form so that I can submit the form with the values checked items as params. Here is the screenshot for clarification:

enter image description here

I added the Save button by extending the Mage class Mage_Adminhtml_Block_Widget_Grid_Container

public function __construct()
    /* other code here */
    $this->_addButton('save', array('label'=> 'Save'));

or Is it the correct way to approach?

Please help


One suggestion is that you could do this with a massaction. This would allow you to work with submissions of a grid giving you all the selected rows on your submit action.

Have a look at the function _prepareMassaction on the block Mage_Adminhtml_Block_Customer_Grid` for what is required for the massactions to work.

In short your need the following:


    $this->getMassactionBlock()->addItem('unique_code', array(
         'label'    => 'Label',
         'url'      => $this->getUrl('*/*/yourMass'),
         'confirm'  => 'Confirmation Message'

Then in your controller you nee the action ion yourMassAction and here you can get all the selected items via:

$selectedIds = $this->getRequest()->getParam('your_entity_id_field');

I know it's old, but I had a problem that required the exact end result as desired in your screen shot and found a solution. I didn't want to use mass-actions as it requires more steps and is generally less intuitive for the user in this case.

Assuming you already have a Namespace_Module_Block_Adminhtml_Grid class and a Namespace_Module_Block_Adminhtml_Form class, create a Namespace_Module_Block_Adminhtml_Form_Container class and set it the container up with the necessary data for it to add your find your form class. (ie, give it the _blockGroup, _controller and _mode if required). This container class is where you can set the title (_headerText - which you've crossed out in the screenshot) and setup the buttons as circled in blue above. Render the container rather than the form in the controller action.

The key part is overwriting the getFormHtml method in the Form class. It uses Varien_Data_Form to render 'Elements'. I imagine there's some 'ideal' way of creating some kind of grid element, or maybe it already exists, but I offer a quicker solution that I've tested to work.

The code I wrote is mainly snatched from the Varien_Data_Form::toHtml method, which we're replacing.

public function getFormHtml() {
    # Loosely copied from Varien_Data_Form::toHtml
    $html = '<form ' . $this->getForm()->serialize($this->getForm()->getHtmlAttributes()) . '>';

    $html .= '<div>';
    if (strtolower($this->getForm()->getData('method')) == 'post') {
        $html .= '<input name="form_key" type="hidden" value="'.Mage::getSingleton('core/session')->getFormKey().'" />';
    $html .= '</div>';

    $html .= $this->getLayout()->createBlock('GRID_BLOCK_PATH')->toHtml();

    $html .= '</form>';
    return $html;

Where 'GRID_BLOCK_PATH' is something like 'namespace/adminhtml_grid'

The only customisation that could be done on the grid is optional, if you don't want to have filters (the content of the filter fields would be sent with the form data, which you almost certainly won't want).


In case it's not clear, you do NOT want a Grid_Container, the nesting structure is

Form_Container -> Form -> Grid
  • Welcome on Magento SE. Posting answers, even when answering old questions, is always appreciated :) – Julien Lachal Nov 4 '15 at 12:33
  • Thanks Julien :D Hopefully it can be of use to someone like me finding it through search engines. – C. Brown Nov 5 '15 at 14:12

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.