Magento – Passing multiselect via form with getRequest()->getParam in admin module

admince-1.8.1.0module

I have an admin module and cannot get the multiselect to pass properly to the observer

        $fieldset->addField('hidden_field', 'hidden', array(
            'name' => 'hidden_field',
            'value' => '1',
        ));

      $fieldset->addField('tester', 'multiselect', array(
        'label' => 'label',
        'class' => 'required-entry',
        'required' => true,
        'name' => 'tester',
        'onclick' => "return false;",
        'onchange' => "return false;",
        'value' => '4',
        'values' => array( '-1'=> array( 'label' => 'Please Select..', 'value' => '-1'),'1' => array( 'value'=> array(array('value'=>'2' ,'label' => 'Option2') , array('value'=>'3' ,'label' =>'Option3') ), 'label' => 'Size'  )), 
        'disabled' => false,
        'readonly' => false,
        'after_element_html' => '<small>Comments</small>', 'tabindex' => 1
      ));            

Now when i save these,

The following is passed ok to the observer

Mage::app()->getRequest()->getParam('hidden_field'); //returns 1

The following multiselect however returns nothing

Mage::app()->getRequest()->getParam('tester'); //returns nothing

if i check the url of the request it looks to me malformed

   [_requestUri:protected] => /index.php/ecms/customer_group/save/key/df52e912e14b962bc721d111729a4e7a?tax_class=3&id=0&hidden_field=1&tester%255B%255D=2&tester%255B%255D=3&tester%255B%255D=4&tester%255B%255D=5&thetitle=mytitle

What part of the process am i getting wrong with this?

Best Answer

A multiselect is passed as name=value1&name=value2&name=value3 therefore only value3 is available inside of php (if you don't want to parse the query string)

To change this, you need an array. Change the name to tester[] and it should work.

like this:

//app/code/core/Mage/Adminhtml/Block/Cms/Block/Edit/Form.php:96
$field =$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),
));