Provides a autocomplete form for ZF2 and Doctrine2.

dev-master 2014-08-14 04:47 UTC

This package is not auto-updated.

Last update: 2024-02-12 23:16:02 UTC


A form element autocomplete for Doctrine 2 and ZF2


DoctrineModule, jQuery, jQueryUi,


Using github

cd vendor
git clone https://github.com/fabiopaiva/Zf2DoctrineAutocomplete

Using composer

php composer.phar require fabiopaiva/zf2-doctrine-autocomplete:dev-master

Copy javascript initializer to your public folder

# if composer
cp vendor/fabiopaiva/zf2-doctrine-autocomplete/data/zf2-doctrine-autocomplete.min.js public/js/
# if github
cp vendor/Zf2DoctrineAutocomplete/data/zf2-doctrine-autocomplete.min.js public/js/

Enable the module

Enable this module in your application.config.php

return array(
    'modules' => array(

Add javascript file to your layout

Copy this file from data folder

echo $this
    ->prependFile($this->basePath() . '/js/zf2-doctrine-autocomplete.min.js');

Create a custom form element with your parameters

This file must be statically configured because is called from Zf2DoctrineAutocomplete engine


namespace Application\Form\Element;
use Zf2DoctrineAutocomplete\Form\Element\ObjectAutocomplete;

class MyAutocompleteElement extends ObjectAutocomplete {

    private $initialized = false;

    public function setOptions($options) {
    if (!$this->initialized) {
        $options = array_merge($options, array(
            'class' => get_class($this),
            'object_manager' => $options['sm']->get('Doctrine\ORM\EntityManager'), // For Doctrine ORM
            // 'object_manager' => $options['sm']->get('doctrine.documentmanager.odm_default'), // For Doctrine ODM (Mongodb)
            'target_class' => 'Application\Entity\MyEntity',
            'searchFields' => array('code', 'description'),
            'empty_item_label' => 'Nothing found',
            'select_warning_message' => 'Select a itemName in list',
            'property' => 'description',
            'orderBy' => array('code','ASC')
        $this->initialized = true;



Add the custom element to your form

        'name' => 'myAutocompleteElement',
        'type' => 'Application\Form\Element\MyAutocompleteElement',
        'options' => array(
            'label' => 'My label here',
            'sm' => $serviceManager // don't forget to send Service Manager
        'attributes' => array(
            'required' => true,
            'class' => 'form-control input-sm'

Add elements dinamically

After add the new element in page, call the initializer: