views:

604

answers:

1

I have a form with (at least) the following two fields:

  • country
  • club

The club is a field that is generated via the ZendX_JQuery_Form_Element_AutoComplete Element, that also generates the following javascript code:

$("#club").autocomplete({"url":"\/mywebsite\/\/mycontroller\/autocomplete"});

I have a database of clubs per country. What I want is that only the clubs are returned for the (user) given country. This list should be retrieved via a remote (ajax) call. The code for that is:

 public function autocompleteAction()
    {
        $request = $this->getRequest();

        $filter = $request->getParam('q');
        $country = $request->getParam('country');

        $clublist = getClubListBySubstring($country, $filter);

        $this->_helper->autoComplete($clublist);
    }

I can probably change the above javascript-code, by-passing the generation done by ZendX_JQuery_Form_Element_AutoComplete and add an extra element to the URL that gets interpreted as parameter country .

But is there a more elegant solution? I've read something about ExtraParams, would that work, and how?

+2  A: 

This is one of those 'gray areas' where using the server to generate the Javascript could work against you. Writing the client-side script yourself would benefit you in a number of ways:

  • You'll learn more about jQuery Autocomplete and what you can do with it.
  • You'll have greater control over the behaviour of the autocompleter.
  • Your solution will be less dependant on the Zend Framework, and will thus be more portable.

I would familiarise myself with the addition parameters on the jQuery side of things, before looking at ZendX_JQuery_Form_Element_AutoComplete as the end-to-end solution. Also, you might want to read the following essay by Joel Spolsky entitled "The Law of Leaky Abstractions" if you haven't already:

http://www.joelonsoftware.com/articles/LeakyAbstractions.html

I've implemented an autocompleter (tied to a ZF controller action) and was fully aware of the existence of ZF's autocomplete helper, but chose not to use it, as I did not want to be in one of those situations where applying a minor tweak would necessitate scrapping the entire solution because the code generation tool on the server doesn't want to play.

Hope that was more helpful than annoying.

karim79
Thanks about the linek to the article, I didn't know it but I agree with the content and I also practice it. But I've had to go around some Zend Framework abstractions before (esp. w.r.t. layout of generated Zend_Forms), but in most cases it's not that nice (duplications). I prefer the 'Zend Framework' way as it's most of the time very nice and clean and I'm not afraid to become dependent upon Zend Framework, as my whole Web Application already is and it saves me a lot of time to develop utility libaries and so on.
Roalt