Rendering an object type as a text field so that I can automatically complete it using jquery

I have a Task object that has nothing to do with the essence of the company (the Company has projects, and each project has Tasks) and this simple form:

class TaskType extends AbstractType
{ 
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       $builder->add('company','entity',array(
                'class' => 'ITMore\FlowBundle\Entity\Company',
                'mapped' => false
            ))
    }
}

and what I want to do is render this field as text , so I can autocomplete it using jquery (friendly user interface). many projects, so I do not want the user to view the entire list). It is assumed that it will work as follows: the user will fill in the company field, then a list of companies that match the input value will be displayed, and after that there is a second input project - which should have prompts with these company projects.

I do not know how to do that. One way that I thought might work is to do this in the controller after checking, but this solution is not very neat

+4
2

DataTransformer.

. .

<?php

namespace Project\Bundle\DuterteBundle\Form\DataTransformer;

use Project\Bundle\DuterteBundle\Entity\City;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;


class CityAutocompleteTransformer implements DataTransformerInterface
{
private $entityManager;

public function __construct(ObjectManager $entityManager)
{
    $this->entityManager = $entityManager;
}

public function transform($city)
{
    if (null === $city) {
        return '';
    }

    return $city->getName();
}

public function reverseTransform($cityName)
{
    if (!$cityName) {
        return;
    }

    $city = $this->entityManager
        ->getRepository('DuterteBundle:City')->findOneBy(array('name' => $cityName));

    if (null === $city) {
        throw new TransformationFailedException(sprintf('There is no "%s" exists',
            $cityName
        ));
    }

    return $city;
}
}

->add('city', 'text', array(
        'label' => 'Type your city',
        //'error_bubbling' => true,
        'invalid_message' => 'That city you entered is not listed',

 $builder->get('city')
      ->addModelTransformer(new CityAutocompleteTransformer($this->entityManager));
+6

Source: https://habr.com/ru/post/1535392/


All Articles