Zend form populate function not working for select

2019-09-14 22:26发布

问题:

I am setting up a zend form as:

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

and in controller passing form's select box listing as:

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

and populating values(after a search request was made) like :

$searchForm->populate($filters);

when I debug $filters array, output was:

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

now the issue is with HTML: I received an output as(when a value was selected in dropdown):

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

Now you can see that every value is selected in dropdown and I always get last selected value. What I am doing wrong or what can be done for it?

回答1:

Try:

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