Adding Checkboxes To An Admin Form

If the requirement is to offer an administrator an assortment of form options from an array it is possible to use a checkbox field.

The following will simply output the data from the array as defined in the

class {Namespace}_{Module}_Block_Adminhtml_{Controller}_Edit_Tab_{Formname} extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);

        $fieldset = $form->addFieldset('{formname}_form',
            array('legend' => 'Legend'));

        $possibleOptionsArray = Mage::getModel('{namespace}/{module}')->getArrayFunction('array', 'name');

        $fieldset->addField('{id}', 'checkboxes', array(
            'label'     => $this->__('Label'),
            'name'      => '{row_name}[]',
            'value'     => $currentValueArray,
            'values'    => $possibleOptionsArray,
            'disabled'  => false
        ));

Important note: ‘name’ => ‘{row_name}[]’,
The name definition should be an array that ends with [].

controller saveAction to contain the following:

foreach ($data as $key => $value)
{
    if (is_array($value))
    {
        $data[$key] = implode(',',$this->getRequest()->getParam($key));
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *