thanks for your help on this. I'm trying to get this going, but not having any luck at all, especially seeing as I haven't used json with zend yet.
I'm getting this error in my controller file. Method "encodeJson" does not exist and was not trapped in __call() public function _prepareautocompletion($data) { $items = array(); foreach ($data as $key => $value) { $items[] = array('label' => $value, 'name' => $value, 'key' => $key); } $final = array( 'identifier' => 'key', 'items' => $items, ); return $this->encodeJson($final); } this is my autocompleteAction in CategoryController.php function autocompleteAction () { $this->getHelper('viewRenderer')->setNoRender(); $request = $this->getRequest(); $category_id = (int)$this->_request->getParam('category_id'); $race = new Race(); $races = $race->getCategoryRaces($category_id); // function to output all races $this->view->races = $this->_prepareautocompletion($races); } Ok, now theres a couple things here i'm unsure of. Firstly because this is setup with the first select box not populated because it is expecting data from a data store, how am I to pass this info into the json request. I'm sure I'm way off track with this one. And Second, why am I getting the json error. Do I need to set something up before hand for this to work. thanks Paul Themselves wrote: > > I have spent WAY too much time getting this exact scenario working using > Zend Dojo forms and an MVC environment, and I plan on building an > extensive > article explaining it all soon, but for now, here's the really quick and > dirty version. I haven't gotten my version perfect yet, I'm still not > happy > with the URLs I'm calling to retrieve the data, but that's not a huge > deal. > Anyway, on with the show. > > First of all, here's your two form elements. > > $this->addElement('FilteringSelect', 'fk_client_id', array( > 'label' => 'Client:', > 'storeId' => 'clientStore', > 'storeType'=> 'dojo.data.ItemFileReadStore', > 'storeParams' => array( 'url' => > '/clients/autocomplete/format/ajax?autocomplete=1&str=*',), > 'autoComplete' => 'false', > 'hasDownArrow' => 'true', > 'id' => 'fk_client_id', > )); > > $this->addElement('FilteringSelect', 'fk_client_contact_id', > array( > 'label' => 'Client contact:', > 'storeId' => 'clientContactStore', > 'storeType'=> 'dojo.data.ItemFileReadStore', > 'autoComplete' => 'false', > 'hasDownArrow' => 'true', > 'id' => 'fk_client_contact_id', > )); > > Now for the javascript, this has to appear somewhere on the page that > contains the form. > > dojo.connect(dijit.byId('fk_client_id'), 'onChange', function () { > dijit.byId('fk_client_contact_id').store = new > dojo.data.ItemFileReadStore({ url: > "/clientcontacts/autocomplete/format/ajax?autocomplete=1&fk_client_id=" + > dijit.byId("fk_client_id").value }); > }); > > > As for the URLs for the Datastores, they are kind of an exercise for the > reader, other than to say they obviously should filter correctly on the > passed parameters, and they have to return JSON. This part was pretty > annoying, but I eventually found that a function like this returns the > correct JSON format: > > public function prepareAutoCompletion($data) { > $items = array(); > foreach ($data as $key => $value) { > $items[] = array('label' => $value, 'name' => $value, 'key' => > $key); > } > $final = array( > 'identifier' => 'key', > 'items' => $items, > ); > return $this->encodeJson($final); > } > > > You pass in to the function an array of id => value pairs, and it will > output the correct JSON for the FilteringSelects. If you use the built in > AutoCompleteDojo helper, it won't use the id from your table as the value > that the form submits, which is pretty much useless. > > Oh, and one more trick, for the Edit action, you are going to need to > include something like this: > > $form->populate($row); > $form->getElement('fk_client_contact_id')->setStoreParams(array( 'url' => > '/clientcontacts/autocomplete/format/ajax?&autocomplete=1&fk_client_id=' . > $form->getElement('fk_client_id')->getValue() )); > > So that it prepopulates the form correctly. > > I promise I'll write up a really impressive document that spells this > whole > thing out in painstaking detail, I'm just absolutely flat out until > christmas, I haven't had any time! > > > On Tue, Dec 9, 2008 at 10:58 AM, Ace Paul <sa...@acewebdesign.com.au> > wrote: > >> >> I have a form, which I would like to use dependent drop downs in. I can't >> seem to find anything about it hear, after looking all morning trying to >> work it out. >> I have one field "race_country" >> when an option is selected I would like to show the cities in that >> country. >> The following is what I have currently in the form, which will show all >> countries and all cities. >> Any help would be great. thanks >> >> $table = new Country(); >> foreach ($table->fetchAll() as $c) { >> $country->addMultiOption($c->country_id, $c->country_name); >> } >> $this->addElement( $country); >> >> $city = new Zend_Form_Element_Select('race_city'); >> $city->setLabel('City') >> ->setRequired(true); >> >> $table = new City(); >> foreach ($table->fetchAll() as $c) { >> $city->addMultiOption($c->city_id, $c->city_name); >> } >> $this->addElement( $city); >> -- >> View this message in context: >> http://www.nabble.com/How-to-set-up-dependant-dropdowns-in-form-tp20907379p20907379.html >> Sent from the Zend Framework mailing list archive at Nabble.com. >> >> > > -- View this message in context: http://www.nabble.com/How-to-set-up-dependant-dropdowns-in-form-tp20907379p21000925.html Sent from the Zend Framework mailing list archive at Nabble.com.