Zend form populate function not working for select

2019-10-31 14:31发布

我为建立一个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>

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

Answer 1:

尝试:

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


文章来源: Zend form populate function not working for select type of element