How to Wrap the Admin Grid in Form in Magento 1.8

adminformgridmagento-1.8

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:

<?php
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'
                    )
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

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

public function __construct()
{
    parent::__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

Best Answer

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->setMassactionIdField('your_entity_id_field');
    $this->getMassactionBlock()->setFormFieldName('your_form_name');

    $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');
Related Topic