数组值是zend表单select元素中的键

时间:2011-11-04 12:29:19

标签: arrays zend-form zend-form-element

我有一个zend表单,在那个表单中我有一个我这样创建的选择框:

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

Model_User :: $ STATUS数组如下所示:

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

输出如下:

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

现在我想要的输出是这样的:

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

长话短说: 我希望数组值是zend形式的关键。如何在不更改状态数组键的情况下实现此目的。

1 个答案:

答案 0 :(得分:0)

只需预处理Model_User :: $ STATUS var。

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

然后设置多选项

$e->addMultiOptions($statuses);