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

7 Answers 7

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.
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

this code helps us in creating an element through addElement rather than createElement above
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,
        ),
    ));

Comments

1

i think this should work:

$form->setDefault('user', 'value'); // Set default value for element

2 Comments

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

To set default values you can try both setDefault or populate

$form->populate( $array_keypair_values );

1 Comment

To increase the quality of your post please include how/why your answer will solve the problem.
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.

Comments

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>);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.