Magento Admin formfield multiselect selected

cpuweb picture cpuweb · Oct 2, 2013 · Viewed 18.1k times · Source

I´m currently developing a custom module for magento thats going to list employees.

I have figured out almost everything. The only thing I got left is how the selected values is going to be highlighted.

The problem I´m having is for the backend.

I got 2 tabs per employee, one for employee data and one tab for magento categories.

1 employee can have 1 or more categories.

The database table that the categories are stored in are a non-eav table.

So my question is What in a multiselect determines which values are selected? As it is now, only one value is selected.

Answer

Ashley Swatton picture Ashley Swatton · Oct 2, 2013

I think you can do this by simply passing in an array of the id's to be selected into the 'value' attribute of the field being added for the multiselect in the _prepareForm() method. Something like the following.

$fieldset->addField('category_id', 'multiselect', array(
          'name' => 'categories[]',
          'label' => Mage::helper('cms')->__('Store View'),
          'title' => Mage::helper('cms')->__('Store View'),
          'required' => true,
          'values' => Mage::getSingleton('mymodule/mymodel')->getMymodelValuesForForm(),
          'value' => array(1,7,10),
));

The id of the form element (e.g. category_id) must not be an attribute in your model, otherwise when the form values get set with $form->setValues() later on, the attribute value will be overwritten.

I normally store multiple selections as a text column separated by commas much like most magento modules handles stores which requires a slightly different approach as shown below.

In the form block for the tab with the multiselect, you firstly define the element to be displayed like so in the _prepareForm() method. You then get the values from the model and set put them into the form data.

protected function _prepareForm()
{
    ...
    $fieldset->addField('store_id', 'multiselect', array(
              'name' => 'stores[]',
              'label' => Mage::helper('cms')->__('Store View'),
              'title' => Mage::helper('cms')->__('Store View'),
              'required' => true,
              'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
    ));
    ...

    if ( Mage::getSingleton('adminhtml/session')->getMymodelData() )
    {
        $data = Mage::getSingleton('adminhtml/session')->getMymodelData();
    } elseif ( Mage::registry('mymodel_data') ) {
        $data = Mage::registry('mymodel_data')->getData();
    }

    $data['store_id'] = isset($data['stores']) ? explode(',', $data['stores']) : array();
    $form->setValues($data);
}

I normally store the selected stores (categories as in your case) in the main model as a text column and comma separated values of ids, hence the explode.

In the controller for for the edit action, I put the model being edited into the mage registry so we can load it and it's values in the step above.

Mage::register('mymodel_data', $model);