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

不问归期 提交于 2019-12-17 16:15:43

问题


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


回答1:


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.



回答2:


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




回答3:


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



回答4:


i think this should work:

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



回答5:


To set default values you can try both setDefault or populate

$form->populate( $array_keypair_values );




回答6:


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.




回答7:


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


来源:https://stackoverflow.com/questions/1588272/zend-framework-set-selected-value-in-select-box-dropdown-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!