Reputation: 91
Custom form type
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityManager;
class NationaliteitidType extends AbstractType
{
private $doctrine;
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
service.yml services:
fw_core.form.type:
class: FW\CoreBundle\Form\Type\NationaliteitidType
arguments:
entityManager: "@doctrine.orm.entity_manager"
error:
Argument 1 passed to FW\CoreBundle\Form\TypeNationaliteitidType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given,
I must have made an type or something else obvious but realy can't find it.
Upvotes: 0
Views: 2782
Reputation: 329
I think (not worked much with Service containers yet, so please bear with me) that you must do the following (already suggested by @Touki):
services.yml:
fw_core.form.type:
class: FW\CoreBundle\Form\Type\NationaliteitidType
arguments:
entityManager: "@doctrine.orm.entity_manager"
And in your class:
public function __construct($entityManager)
{
$this->em = $entityManager;
}
You see, the __construct argument (entityManager in this case) must match the same name as before the colon in the services.yml.
edit: You might also need to do something with tags, see here too: http://symfony.com/doc/current/book/service_container.html
Upvotes: 0
Reputation: 1
In the controller use PrintelBundle\Form\XXXType;
class XXXController extends Controller
{
....
public function newAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entity = new XXX();
$form = $this->createForm(new XXXType($em), $entity);
....
And in the formType
class XXXType extends AbstractType
{
private $em;
public function __construct($em)
{
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $this->em;
........ done
Upvotes: 0
Reputation: 29912
Why do you need to inject an entity manager? If you want to populate a field based with some infos retrieved from db, you should simply make something like
$qb_function = function(EntityRepository $rep) use ($bar_parameter) {
return $rep->createQueryBuilder('s')
->where('s.bar = :bar')
->setParameter('bar',$bar_parameter);};
}
$builder->add('foo','entity',array('query_builder')=>qb_function);
Upvotes: 0
Reputation: 715
In your services.yml, you can't name your future variables, so try something like this :
services :
fw_core.form.type:
class: FW\CoreBundle\Form\Type\NationaliteitidType
arguments:
- "@doctrine.orm.entity_manager"
Upvotes: 2