Setting default selected option for Zend_Form_Element_Select

Sejanus picture Sejanus · Aug 15, 2010 · Viewed 12.5k times · Source

Possible Duplicate:
Zend Framework - Set 'selected' value in select box dropdown list

I have a Zend_Form with Zend_Form_Select element. I populate it from array (code inside the Application_Form_MyForm extends Zend_Form class):

$options = array('first option', 'second option', 'third option');
$this->getElement('mySelect')->addMultiOptions($options);

How can I choose which value is gonna be selected automatically, as in "<option value="second option" selected="selected">second option</option>" ? Thanks!

Answer

Keyne Viana picture Keyne Viana · Aug 16, 2010

To populate all form values, you can call $form->populate($dataAsArray);

If you want to set a default value, you can call $select->setValue('valueHere'); If it were a checkbox, instead of pass one sigle value you would pass an array of selected indexes.

See this question: Zend_Form_Element_MultiSelect element definition