ioalessio / autosuggestbundle
Autosuggest widget for Symfony2 Form
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Installs: 982
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Language:JavaScript
Type:symfony-bundle
Requires
- php: >=5.3.0
- symfony/framework-bundle: >=2.1
This package is not auto-updated.
Last update: 2024-01-20 11:17:20 UTC
README
Add IoAutosuggestBundle in your composer.json:
{ "require": { "ioalessio/autosuggestbundle": "dev-master" } }
Now tell composer to download the bundle by running the command:
$ php composer.phar update
Composer will install the bundle to your project's vendor/ioalessio
directory.
Step 2: Enable the bundle
Enable the bundle in the kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Io\AutosuggestBundle\IoAutosuggestBundle(), ); }
Step 3: Configure the bundle
Add javascript files in your page (you can also put at the end of page)
- libs/boostrap/bootstrap-typehead.js
- bundles/ioautosuggest/twitter-bootstrap-typeahead.js #this file extends original twitter bootstrap typehead file
- bundles/ioautosuggest/autosuggest.js
IMPORTANT: bootstrap-typehead.js can be downloaded here: http://twitter.github.com/bootstrap/javascript.html#typeahead
Add widget code in your form template file
{# fields.html.twig #} {% extends 'form_div_layout.html.twig' %} {% block autosuggest_selector_widget %} {% spaceless %} {{ form_widget(form.autosuggest, { 'attr' : { 'class': 'ajax-typeahead ', 'data-value': form.value.vars['id'], 'data-link' : form.vars['attr']['url'] } } ) }} {{ form_widget(form.value) }} {{ form_rest(form) }} {% endspaceless %} {% endblock autosuggest_selector_widget %}
Step 3: Include widget in a Form
#FORM CLASS public function buildForm(FormBuilderInterface $builder, array $options) { parent::buildForm($builder, $options); $builder->add('field', 'autosuggest_selector', array( 'entityName' => 'Acme\DemoBundle\Entity\MyEntity', 'autocompleteMethod' => 'autocomplete', 'valueMethod' => 'id', 'route' => 'autosuggest_typehead' )); ... } #AUTOCOMPLETE CONTROLLER /** * @Route("/autosuggest.{_format}", name="autosuggest_typehead", defaults={"_format"="json"}) */ public function autosuggestAction() { $query = $this->getRequest()->get('query'); $data = $this->getDoctrine()->getEntityManager()->createQuery("SELECT e.id, e.name AS name FROM AcmeDemoBundle:MyEntity e WHERE e.name LIKE :query") ->setParameter('query', "%".$query."%") ->getArrayResult(); // array must countain 'id' and 'name' $response = new Response(json_encode($data)); return $response; }