0

How to create autocomplete in symfony2 that will load users from local database (one input) without using any bundle? Here's my users entity :

<?php

namespace Me\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="Me\MainBundle\Repository\UsersRepository")
 * @ORM\Table(name="users")
 */

class Users
{
     /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Assert\NotBlank(
     *      message = "Users first name field can not be blank!",
     *      groups={"registration"}
     * )
     * @Assert\Length(
     *      min = 3,
     *      minMessage = "First name must be at least 3 characters long!",
     *      groups={"registration","search"}
     * )
     * @ORM\Column(type="string", length=255, nullable=false, name="first_name")
     */
    protected $firstName;

    /**
     * @Assert\NotBlank(
     *      message = "Users last name field can not be blank!",
     *      groups={"registration"}
     * )
     * @Assert\Length(
     *      min = 3,
     *      minMessage = "Last name must be at least 3 characters long!",
     *      groups={"registration","search"}
     * )
     * @ORM\Column(type="string", length=255, nullable=false, name="last_name")
     */
    protected $lastName;

//relationship variables:

    /**
     * @ORM\OneToMany(targetEntity="UsersSkillLevel", mappedBy="userId")
     **/
    protected $usersSkillLevels;

    public function __construct() {
        $this->usersSkillLevels = new ArrayCollection();
    }

//getters, setters:


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set firstName
     *
     * @param string $firstName
     * @return Users
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

    /**
     * Get firstName
     *
     * @return string 
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * Set lastName
     *
     * @param string $lastName
     * @return Users
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    /**
     * Get lastName
     *
     * @return string 
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * Add usersSkillLevels
     *
     * @param \Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels
     * @return Users
     */
    public function addUsersSkillLevel(\Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels)
    {
        $this->usersSkillLevels[] = $usersSkillLevels;

        return $this;
    }

    /**
     * Remove usersSkillLevels
     *
     * @param \Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels
     */
    public function removeUsersSkillLevel(\Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels)
    {
        $this->usersSkillLevels->removeElement($usersSkillLevels);
    }

    /**
     * Get usersSkillLevels
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUsersSkillLevels()
    {
        return $this->usersSkillLevels;
    }
}

And search form:

$searchForm = $this->createFormBuilder()
            ->setMethod('POST')
            ->add('user_name', 'text', array('label' => 'User\'s name: ','attr' => array('placeholder' => 'Enter Users first or last name')))
            ->add('searchUsers', 'submit', array('label' => 'Search for employee'))
            ->getForm();

It should pop when first or last name is written after 3 letters.

Thank you in advance

Matiww
  • 3
  • 3

1 Answers1

0

I think this will help you:

Two things you need to do:

1) Create a Data Transformer

hich will let you use an input field instead of the default dropdown to the linked entity.

The Data Transformer basically transforms your input (in your case a username string) to actual user object that you can store with your entity. It will let you define an input field instead of the default dropdown to the linked entity. Have a look at the documentation, it's pretty straight forward.

2) Create the Ajax autocomplete

This can be done using any library you want (jQuery, Prototype etc.) on the client side, and server side you handle this in your Controller action and replies with either JSON or a rendered template. There are plenty of tutorials for this out there. The Ajax result could include a link to create a new user, if none is found.

The Data Transformer is what you need to focus on, and I would get that working before working on the UI.

Refer bellow :

How to add an autocomplete field in forms Symfony2?

Community
  • 1
  • 1
kailash sharma
  • 356
  • 1
  • 12