Zend Framework - Set 'selected' value in select box dropdown list
Asked Answered
C

7

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

Cytokinesis answered 19/10, 2009 at 11:55 Comment(0)
C
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.
Cytokinesis answered 19/10, 2009 at 13:31 Comment(0)
I
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..

Impudicity answered 18/1, 2010 at 6:48 Comment(1)
this code helps us in creating an element through addElement rather than createElement aboveMaybellmaybelle
C
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,
        ),
    ));
Cabala answered 4/12, 2014 at 11:56 Comment(0)
O
1

i think this should work:

$form->setDefault('user', 'value'); // Set default value for element
Obtrusive answered 19/10, 2009 at 12:12 Comment(2)
That doesn't seem to work. I have set 'value' to the corresponding value of the <option> that i want to apply selected="selected" to but it does not get set to the selected state.Cytokinesis
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.Burnell
S
0

To set default values you can try both setDefault or populate

$form->populate( $array_keypair_values );

Somme answered 3/8, 2012 at 20:49 Comment(1)
To increase the quality of your post please include how/why your answer will solve the problem.Ichabod
G
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.

Gynandry answered 20/12, 2012 at 11:49 Comment(0)
C
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>);
Comfit answered 10/7, 2016 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.