Answer the question
In order to leave comments, you need to log in
How to make a text field to select an entity in a form in Symfony 3?
In Symfony 2 it was like this:
<?php
namespace AppBundle\Form\Type;
use AppBundle\Form\DataTransformer\EntityToNumberTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OfferImageType extends AbstractType
{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('main', 'hidden')
->add('sort', 'hidden')
->add('image', 'hidden');
$builder->get('image')
->addModelTransformer(new EntityToNumberTransformer($this->manager, 'AppBundle:File'));
}
}
Answer the question
In order to leave comments, you need to log in
Pass EntityManager as options:
/**
* @inheritDoc
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('manager');
$resolver->setAllowedTypes('manager', array(
ObjectManager::class
));
}
$form = $this->createForm(FooType::class, $object, array(
'manager' => $this->getDoctrine()->getManager(),
));
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->get('image')
->addModelTransformer(new EntityToNumberTransformer($options['manager'], 'AppBundle:File'));
}
app.type.foo:
class: %app.type.foo%
arguments:
- "@doctrine.orm.entity_manager"
tags:
- { name: form.type }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question