#README#
##Introduction##
This is a Symfony v2.1 Bundle that provides a Bootstrap Typeahead autocomplete widget for use in forms. An enhanced version of the Typeahead component is included that adds several new features and enhancements.
###Enhanced Typeahead Features###
- Supports JSON objects
- Caches results
- Delays AJAX request to reduce server requests
- Properly handles pasting via mouse
- Includes an
AJAX Loadericon
###Screenshots###
This example shows a form field that allows a single name to be entered.
This example shows a form field that allows multiple names to be entered. Clicking on a name link removes the entity. The entity in the backend is actually an ArrayCollection and automatically allows adding/removing entities from the list.
##How to install##
Note: This bundle requires jQuery and Bootstrap to be installed in your environment but does not include them directly. I suggest using the mopa/bootstrap-bundle which can help with this for you.
- Add
lifo/symfony-typeahead-bundleto your projectscomposer.json"requires" section:
{
// ...
"require": {
// ...
"lifo/symfony-typeahead-bundle": "dev-master"
}
}- Run
php composer.phar update lifo/symfony-typeahead-bundlein your project root. - Update your project
app/AppKernel.phpfile and add this bundle to the $bundles array:
$bundles = array(
// ...
new Lifo\TypeaheadBundle\LifoTypeaheadBundle(),
);- Update your project
app/config.ymlfile to provide global twig form templates:
twig:
form:
resources:
- 'LifoTypeaheadBundle:Form:fields.html.twig'
-
Update your site twig template to initialize the typeahead javascript. There are two options here.
- In your template add the following twig function call anywhere:
{{ lifo_typeahead_init() }}- Or, if you want to combine the javascript with your main site using assetic you can do something like this:
{% block javascripts %} {% javascripts filter='?yui_js' output='js/site.js' '@MyBundle/Resources/public/js/jquery-1.9.1.min.js' '@MyBundle/Resources/public/js/bootstrap.min.js' '@LifoTypeaheadBundle/Resources/public/js/typeaheadbundle.js' '@MyBundle/Resources/public/js/site.js' %} <script src="{{ asset_url }}"></script> {% endjavascripts %} {% endblock %}- (Optional) Add
LifoTypeaheadBundleto yourapp/config/config.yml. This is only required if you want to include the typeahead javascript as part of your main site JS using assetic.
assetic: bundles: [ 'LifoTypeaheadBundle' ]
##How to use##
Using the typeahead control is extremely simple. The available options are outlined below:
$builder->add('user', 'entity_typeahead', array(
'class' => 'MyBundle:User',
'render' => 'username',
'route' => 'user_list',
));- Required Options
classis your entity class.renderis the property of your entity to display in the autocomplete menu.routeis the name of the route to fetch entities from. The controller matching the route will receive the following parameters viaPOST:queryThe query string to filter results by.limitThe maximum number of results to return.
- Optional Options
route_paramsExtra parameters to pass to theroute.minLengthMinimum characters needed before firing AJAX request.itemsMaximum items to display at once (default: 8)delayDelay in milliseconds before firing AJAX (default: 250)loadingIconUrlImage icon to display during AJAX request.multipleIf true the widget will allow multiple entities to be selected. One at a time. This special mode creates an unordered list below the typeahead widget to display the selected entities.callbackCallback function (or string) that is called when an item is selected. Prototype:function(text, data)wheretextis the label of the selected item anddatais the JSON object returned by the server.
###AJAX Response###
The controller should return a JSON array in the following format. Note: id and value properties are required and you may include any other properties that can potentially be used within the template.
[
{ id: 1, value: 'Displayed Text 1' },
{ id: 2, value: 'Displayed Text 2' }
]###Template###
Your form template might look something like this (The screenshots above used this template bit).
Note: The widget_addon attribute is a mopa/bootstrap-bundle attribute.
{{ form_row(form.name) }}
{{ form_row(form.owner, { attr: { placeholder: 'Search for user ...'}, widget_addon: {type: 'append', 'icon': 'user'}}) }}
{{ form_row(form.users, { attr: { placeholder: 'Add another user ...'}, widget_addon: {type: 'append', 'icon': 'user'}}) }}##Notes##
This bundle renders its form elements in standard Symfony style. You will have to override the form blocks to get the proper Bootstrap styles applied. I strongly suggest something like mopa/bootstrap-bundle that will override the symfony form templates with proper Bootstrap versions automatically for you.

