1

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.

  • see this stackoverflow.com/questions/19138173/… – Manashvi Birla Jul 22 '15 at 5:50
  • Tks, but already tried it, even statically passing comma separated values or single dimension array, doesn't work. I think I already mentioned that in my question. – H.D. Jul 22 '15 at 5:58
  • Did you set the values for namespace_module using Mage::register? – eetzen Jul 22 '15 at 6:43
  • Yes did that too, I think I have to override the varien data form element multiselect file for this, as the value attribute doesn't seem to work on core level, can you check the varien file of multiselect, I tried it but doesn't work at all. Maybe you can tell me if I forgot anything, though if I add custom attribute in multiselect and call that in varien multiselect file then it's values are displayed fine, but not for "value" attribute of field – H.D. Jul 22 '15 at 6:49
2

This is how your multi-select field should be defined.

$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()
));

Here the most vital part in this context is values of they keys value and values. values key is supposed to hold an array which should pass all possible options of your multi-select field, whereas value 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:

  1. 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.

  2. Make sure value holds already existing option-values in comma-separated format.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.