20

I am adding a select element to a Zend_Form instance as follows:

  $user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
  foreach($users as $u)
        {
            if($selected == $u->id)
            {
                $user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
                //*** some way of setting a selected option? selected="selected"

            }
            else
                $user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
        }

I have been searching the docs but cannot find a simple way of pre-setting an option of the select element to 'selected'.

Tom
  • 1,115
  • 1
  • 8
  • 13

7 Answers7

53

I've just worked out how to do it.

You have to use the setValue() method of the element:

$user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
    foreach($users as $u)
        $user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);

$user->setValue($selected); //$selected is the 'value' of the <option> that you want to apply selected="selected" to.
Tom
  • 1,115
  • 1
  • 8
  • 13
30
$form->addElement('select','foo',
array(
        'label'        => 'ComboBox (select)',
        'value'        => 'blue',
        'multiOptions' => array(
            'red'    => 'Rouge',
            'blue'   => 'Bleu',
            'white'  => 'Blanc',
        ),
    )
);

As above, you can use 'value' => 'blue' for making 'blue' => 'Bleu' selected.

I hope this will help you..

Raj
  • 22,346
  • 14
  • 99
  • 142
Tony
  • 309
  • 3
  • 2
  • this code helps us in creating an element through addElement rather than createElement above – Raj Feb 10 '11 at 05:16
7

In Zend Framework 2 set the 'value' attribue. For example to default the Select to 'Yes':

    $this->add( array(
        'name'     => 'isFlexible',
        'type'     => 'Select',
        'options'  => array(
             'label'            => 'Is it flexible?'
            ,'label_attributes' => array( 'placement' => 'APPEND')
            ,'value_options'    => array(
                    ''  => 'Select Below',
                    '0' => 'No',
                    '1' => 'Yes',
                    '2' => 'N/A',
            ),
        ),
        'attributes' => array(
            'id'     => 'is_flexible',
            'value'  => 1,
        ),
    ));
Onshop
  • 2,915
  • 1
  • 27
  • 17
1

i think this should work:

$form->setDefault('user', 'value'); // Set default value for element
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • That doesn't seem to work. I have set 'value' to the corresponding value of the – Tom Oct 19 '09 at 13:23
  • 1
    `setDefault()` is a form method. Tom's solution, `setValue()`, is an element method. It just depends which object you're working with when setting the value. – Sonny Sep 21 '10 at 19:11
0

To set default values you can try both setDefault or populate

$form->populate( $array_keypair_values );

0

I just try following code to show drop-down value as selected from controller and it work fine.

$user->setValue($value); //$value is the 'value' of the and $user is the element of from.

Gokul Shinde
  • 957
  • 3
  • 10
  • 30
0

The mentioned solutions won't work for Zend Framework 2, For those who use Zf2, I suggest using the following instruction to set a default value:

    $formX->get('<Select element Name>')->setValue(<the id of the selected item>);
Yazid Erman
  • 1,166
  • 1
  • 13
  • 24