How to save the id of a Zend_Dojo_Form_Element_ComboBox not the text
Can anyone tell me how I can save the ID of a Dojo ComboBox element in Zend. Sorry if this is simple but I can't figure out the answer.
My combobox code is as follows
// Create a autocomplete input for counties
$county = new Zend_Dojo_Form_Element_ComboBox('county');
$county->setLabel('County');
$county->setOptions(array(
'autocomplete' => true,
'storeId' => 'countyStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array('url' => "/clients/client/autocomplete"),
'dijitParams' => array('searchAttr' => 'county')))
->setRequired(true)
->addValidator('NotEmpty', true)
->addFilter('HTMLEntities')
->addFilter('StringToLower')
->addFilter('StringTrim');
The code for my autocompleteAction is as follows
public function autocompleteAction()
{
// disable layout and view rendering
$this->_helper->layout->disableLayout();
$this->getHelper('viewRenderer')->setNoRender(true);
// get list of breed names from the breeds table
$qry= Doctrine_Query::create()
->from('PetManager_Model_Counties c');
$result=$qry->fetchArray();
//generate and return JSON string countiesID being the primary key of the table
$data = new Zend_Dojo_Data('c开发者_运维技巧ountiesID',$result,'countiesID');
echo $data->toJson();
}
The form is an input form for saving new clients. The table that it manipulates has a field county that references a table counties and hence only accepts Int values.
I want to save the primary key of the county 'countiesID' from that selected in the Dojo ComboBox, currently only the text selected is passed i.e. 'Dublin' not '10', which of course throws an exception when I try to save the record.
I know how to do this with a standard combobox in that I would create a function that would query the counties table and use a foreach array to add the primary key and county name to the form via addMultiOption but I can figure out how to do this when using the Dojo autocomplete combobox.
I've actually sorted this with help from the Zend Community.
You actually have to use FilteringSelect instead of ComboBox to save the record ID as follows
// Create a autocomplete select input for counties
$county = new Zend_Dojo_Form_Element_FilteringSelect('county');
$county->setLabel('County');
$county->setOptions(array(
'autocomplete' => true,
'storeID' => 'countyStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array('url' => "/clients/client/autocomplete"),
'dijitParams' => array('searchAttr' => 'county')))
->setRequired(true)
->addValidator('NotEmpty', true)
->addFilter('HTMLEntities')
->addFilter('StringToLower')
->addFilter('StringTrim');
I hope this helps you ZendNewbie and anyone else new to the framework.
精彩评论