This post describes steps required to create jQuery/AJAX input text field with autocomplete feature using Symfony php framework. It is assumed that you know how to create a web form, and install Symfony plugins, so that part won’t be explained here.
A Symfony plugin sfFormExtraPlugin is required, and, of course, jQuery (I’m using sfJqueryReloadedPlugin).
In form class add this to enable jQuery Autocomplete widget:
# lib/form/Doctrine/YourFormName.class.php $city = new sfWidgetFormDoctrineJQueryAutocompleter(array( 'url' => '/autocomplete', 'model' => 'City', 'value_callback' => 'findOneById' )); $this->setWidget('city_id', $city);
Note that widget requires parameters url, model
and value_callback
to work properly. Here is a brief overview of their meanings:
url
: URL name on which Autocomplete widget will send queries for possible valuesmodel
: Name of model that will be used for transforming ID’s to text values of autocomplete optionsvalue_callback
: Method that will be used for transforming ID’s to text values
You’ll need to create action to handle Autocompleter’s requests. Create one in some module like this:
# apps/frontend/modules/MODULE_NAME/actions/actions.class.php public function executeAutocomplete(sfWebRequest $request) { $result = Doctrine_Core::getTable('City') ->findCityByName($request['q']) ->toKeyValueArray('id', 'title'); return $this->renderText(json_encode($result)); }
This Action will search Table “City” for entries that resemble search request and return them in a JSON array required by Autocomplete plugin.
Add a route to routing.yml file:
autocomplete: url: /autocomplete param: { module: MODULE_NAME, action autocomplete}
Finally, add a method in model class to handle queries required by your action method:
public function findCityByName($name, $limit=10) { return Doctrine_Core::getTable('City') ->createQuery('c') ->where("c.title LIKE '%{$name}%'") ->limit($limit) ->execute(); }
thanks a lot. saved my time.
I followed these steps but when I input a word the the elements to autocomplete doesn’t show.
I installed sfFormExtraPlugin-1.1.3 and sfJqueryReloadedPlugin-1.4.3 and my is:
public function configure()
{
$item = new sfWidgetFormDoctrineJQueryAutocompleter(array(
‘url’ => ‘Items/autocomplete’,
‘model’ => ”Items’,
‘value_callback’ => ‘findOneById’
));
$this->setWidget(‘idItem’, $item);
}
The action autocomplete:
public function executeAutocomplete(sfWebRequest $request){
$result = Doctrine_Core::getTable(‘Items’)
->FindByname($request[‘q’])
->toKeyValueArray(‘iditem’, ‘name’);
return $this->renderText(json_encode($result));
}
And my method in the table class
public function FindByname($name, $limit=10){
return Doctrine_Core::getTable(‘Items’)
->createQuery(‘c’)
->where(“c.name LIKE ‘%{$name}%'”)
->limit($limit)
->execute();
}
Can anybody help me, thanks.
Thanks, this saved me some time remembering how this widget worked
Hey Is it possible to create this widget to be field dependent , for eg I want the list of cities populated after I get the country id from a previous field?
ALso will this accept new city values, how do i configure that??