<?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'));
}
}
/**
* @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 }