Zend表单填充函数不适用于select类型的元素

时间:2012-04-10 12:37:14

标签: zend-framework zend-form zend-form-element

我正在设置一个zend表格:

class Application_Form_Ticket_Search extends Zend_Form
{
    private $_itemList;

    public function setItemList($itemList)
    {
        $this->_itemList[0] = 'Select Item';      // Default selected value
        foreach($itemList AS $key => $value)
        {
            $this->_itemList[$value['REFERENCE']] = $value['DESCRIPTION'];
        }
    }

    // Initializes search form
    public function init()
    {           
        // Settings POST type request
        $this->setName('search')->setMethod('GET');        

        $this->addElement('select', 'item',
                array(  'label'     =>    'Item',
                        'filters'   =>    array('StringTrim'),
                        'multiOptions' => $this->_itemList
        ))->removeDecorator('HtmlTag');

        $this->addElement('submit', 'search',
                array(  'class'     =>    'btn btn-primary btn-small',
                        'label'     =>    'Search',
                        'style'     =>    'margin-left:70px;')
        )->removeDecorator('HtmlTag');        
    }    
}

并在控制器传递表单的选择框列表中:

$searchForm = new Application_Form_Ticket_Search(array('itemList' => $itemList));

并填充值(在发出搜索请求之后),如:

$searchForm->populate($filters);

当我调试$filters数组时,输出为:

array(1) { ["item"]=> string(36) "201031999999992010051000000170430719" } 

现在问题出在HTML上:我收到了一个输出(当在下拉列表中选择了一个值时):

<select name="item" id="item">
<option value="0" label="Select Item">Select Item</option>
<option value="201031999999992010051000000170430719" label="ELITE FRT CHWG N/AV GUM ST 15CT" selected="selected">ELITE FRT CHWG N/AV GUM ST 15CT</option>
<option value="201031999999992010051000000170430869" label="Consolidator" selected="selected">Consolidator</option>
<option value="201031999999992010051100000170450719" label="DAVID PUMPKIN SEEDS" selected="selected">DAVID PUMPKIN SEEDS</option>
<option value="201031999999992010051100000170450739" label="Consolidator" selected="selected">Consolidator</option>
<option value="201031999999992010051000000170430809" label="GARDETTO ORIGINAL" selected="selected">GARDETTO ORIGINAL</option>

现在你可以看到在下拉列表中选择了每个值,我总是得到最后选择的值。 我做错了什么或者可以为它做些什么?

1 个答案:

答案 0 :(得分:0)

尝试:

class Application_Form_Ticket_Search extends Zend_Form
{
    public function setItemList($itemList)
    {
        $items = array('Select Item');      
        foreach($itemList as $key => $value) {
            $items[$value['REFERENCE']] = $value['DESCRIPTION'];
        }
        $this->getElement('item')->setMultiOptions($items);
    }

    // Initializes search form
    public function init()
    {           
        // Settings POST type request
        $this->setName('search')->setMethod('GET');        

        $this->addElement('select', 'item',
                array(  'label'     =>    'Item',
                        'filters'   =>    array('StringTrim')
        ))->removeDecorator('HtmlTag');

        $this->addElement('submit', 'search',
                array(  'class'     =>    'btn btn-primary btn-small',
                        'label'     =>    'Search',
                        'style'     =>    'margin-left:70px;')
        )->removeDecorator('HtmlTag');        
    }    
}

$searchForm = new Application_Form_Ticket_Search();
$searchForm->setItemList(array('itemList' => $itemList));