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 <[EMAIL PROTECTED]> 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.
>
>

Reply via email to