简体   繁体   中英

array value to be the key in the zend form select element

I have a zend form and in that form I have a select box which I create like this:

$e = new Zend_Form_Element_Select('user_status');
$e->setLabel('Status');
$e->addMultiOptions(Model_User::$STATUS);
$e->setRequired(false);
$this->addElement($e);

The Model_User::$STATUS array looks like this:

public static $STATUS = array(
    1   =>  'creating user',
    2   =>  'creating book',
    3   =>  'book created',
    4   =>  'book send'
);

The output is as following:

<select name="user_status" id="user_status">
   <option value="0" label="creating user">creating user</option>
   <option value="1" label="creating book" selected="selected">creating book</option>
   <option value="2" label="book created">book created</option>
   <option value="3" label="book send">book send</option>
</select>

Now what I want the output to be like is like this:

<select name="user_status" id="user_status">
   <option value="creating user" label="creating user">creating user</option>
   <option value="creating book" label="creating book" selected="selected">creating book</option>
   <option value="book created" label="book created">book created</option>
   <option value="book send" label="book send">book send</option>
</select>

To make a long story short: I want the array value to be the key in the zend form. How do I achieve this without changen the status array keys.

Just pre-process Model_User::$STATUS var.

$statuses = array();
foreach (Model_User::$STATUS as $status) {
    $statuses[$status] = $status;
}

And then set multioptions

$e->addMultiOptions($statuses);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM