1

I created a form in zend framework. Here I want to set selected value from option while editing record. For example record have group id value 3.
This works fine in while adding record and while editing record i am not getting selected to third option.
I wrote below code but its not working.

   $group_id = $this->createElement('select','group_id');
   $group_id->setLabel('Category:')
               ->addMultiOptions(array(
                '1' => 'A',
                '2' => 'B', 
                '3' => 'C',
                '4' => 'D'                       
                    ))               
           ->setRequired(true)
           ->setDecorators(array('ViewHelper','Errors'));
   $group_id->setValue(3);

adminController.php

$id =  $this->_request->getParams('id');        
$row = $content->find($id)->toArray();
$form->populate($row[0]);

this populates data in all the fields only not setting selected for drop down. Here 3rd option should show selected but always 1st option is showing selected.

Please let me know if i am missing anything here.

9
  • 1
    Your key is string(3) & you passing int(3) in setValue, try giving it like $group_id->setValue('3'); Commented Jan 15, 2014 at 9:11
  • 1
    I copy/pasted your code and it works as it should, so problem has to be in different place. Commented Jan 15, 2014 at 9:15
  • Please check this link for your problem : stackoverflow.com/questions/1588272/… Commented Jan 15, 2014 at 9:21
  • 1
    your code is working fine for me! Commented Jan 15, 2014 at 10:14
  • sorry its working fine, I was checking when I tried to edit detail.In case of $form->populate($abc[0]); I think It should show selected when we we are editing any record too. Commented Jan 15, 2014 at 10:31

1 Answer 1

1

try without setting any selected value

$group_id = $this->createElement('select','group_id');
$group_id->setLabel('Category:')
           ->addMultiOptions(array(
            '1' => 'A',
            '2' => 'B', 
            '3' => 'C',
            '4' => 'D'                       
                ))               
       ->setRequired(true)
       ->setDecorators(array('ViewHelper','Errors'));

you can also do like below

$form->addElement('select','group_id',
array(
        'label'        => 'Category:',
        'multiOptions' => array(
            '1'  => 'A',
            '2'  => 'B',
            '3'  => 'C',
            '4'  => 'D',
        ),
    )
);
Sign up to request clarification or add additional context in comments.

4 Comments

I tried but its not showing selected to third option. Hoe to troubleshoot here?
try this without setting any value, it will work by default with populate a row array to the form
I have been tested it doesn't select selected option.
but it is working in my case and used version(1.11.10). which version you are using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.