DRAJI
DRAJI

Reputation: 1869

How to add value dynamically to multiselect attribute in magento?

I have used the following code for to add multiselect attribute in mine custom module Form

$fieldset->addField('multiselect2', 'multiselect', array(
      'label'     => Mage::helper('checkout')->__('Select Type2'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'title',
      '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'    
                                       ),
                            '2' => array(
                                            'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
                                            'label' => 'Color'   
                                       ),                                         

                       ),
      'disabled' => false,
      'readonly' => false,
      'after_element_html' => '<small>Comments</small>',
      'tabindex' => 1
    ));

Its working Good. But i want to set the values as dynamically. That means I want to display all category list under this multiselect attribute. I have searched lot of things. But i cant get. Can we use foreach loop for adding value to that attribute? or have any other way to do this? Please help me guys!

Upvotes: 0

Views: 1887

Answers (1)

denSandman
denSandman

Reputation: 166

You can prepare values array with any data before and then use it in your form. For categories it's smth like this:

$categories = Mage::getModel('catalog/category');
$values = array();
foreach ($categories as $category) {
    $values[] = array('label' => $category->getName(), 'value' => $category->getId());
}

Hope it's clear and works for you.

Upvotes: 3

Related Questions