I am using Magento version 1.9.1.0 CE and I have developed a module involving an admin form having a multiselect field.
I have been able to populate the multiselect field with values when in New entry mode but if I have saved the data and then when I go to edit the same data then in edit mode the saved values are not displayed pre-selected.
Means selected='selected' is not working in admin multiselect edit form.
Below is my form code:
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save'),
'method' => 'post',
));
$form->setUseContainer(true);
$this->setForm($form);
$helper = Mage::helper('namespace_module');
$fieldset = $form->addFieldset('display', array(
'legend' => $helper->__('Change Default Values'),
'class' => 'fieldset-wide'
));
$fieldset->addField('fieldIds', 'multiselect', array(
'name' => 'field_ids[]',
'label' => $helper->__('Field Labels'),
'title' => $helper->__('Field Labels'),
'class' => 'required-entry',
'required' => true,
'index' => 'fieldIds',
'value' => 20,19,17,
'values' => Mage::helper('namespace_module')->getProductsList()
));
/*if (Mage::registry('namespace_module')) {
$form->setValues(Mage::registry('namespace_module')->getData());
}*/
$regData = Mage::registry('namespace_module')->getData();
$formVals = Mage::registry('namespace_module')->getData();
if(!empty($regData)) $data = $regData;
echo "<pre/>";print_r($data);
if(!empty($data['field_ids'])) {
$formVals['field_ids'] = explode(',',$data['field_ids']);
$formVals['field_ids'] = array($formVals['field_ids']);
echo "<pre/>";print_r($formVals);
$form->setValues($formVals);
}
## $this->setForm($form);
return parent::_prepareForm();
}
I have also checked by debugging the /lib/Varien/Data/Form/Element/Multiselect.php file, the value element of array shows nothing, even if I pass comma-separated values or single dimension array.
Help me out here somebody.
Best Answer
This is how your multi-select field should be defined.
Here the most vital part in this context is values of they keys
value
andvalues
.values
key is supposed to hold an array which should pass all possible options of your multi-select field, whereasvalue
key is also supposed to hold an array, but only those option-values which needs to be selected (that should be comma separated too).If you are passing data like that and still you are not getting the desired result, then there are some other possibilities which you need to check here is:
Check whether fieldset uses default mutli-select element field class (ie
/lib/Varien/Data/Form/Element/Multiselect.php
) in order to generate the multi-select field. It is possible to set a custom elemenent renderer class for the same.Make sure
value
holds already existing option-values in comma-separated format.Hope that helps.