0

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.

Bart
  • 91
  • 3
  • 11
  • You've named the argument and it doesn't match the variable name you've defined in __construct. Either use `- "@doctrine.orm.entity_manager"` or use `em: "@doctrine.orm.entity_manager` – Touki Apr 18 '14 at 13:30
  • fw_core.form.type: class: FlexWeb\CoreBundle\Form\Type\NationaliteitidType arguments: ["@doctrine.orm.entity_manager"] same error – Bart Apr 18 '14 at 13:34
  • The service declaration mentions `...\Form\Type\Nationalite`. The error says `...\Form\TypeNationalite` is that a typo ? Do you have such a class? If nothing does, try with `@doctrine.orm.default_entity_manager` – Touki Apr 18 '14 at 14:32

4 Answers4

2

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"
mneute
  • 686
  • 1
  • 6
  • 14
  • 1
    Tried the suggestions, still get Missing argument 1 for FW\CoreBundle\Form\Type\NationaliteitidType::__construct() – Bart Apr 18 '14 at 14:02
  • I am also using this config which is working fine `fw_core.menu_builder: class: FW\CoreBundle\Menu\MenuBuilder arguments: ["@knp_menu.factory", "@doctrine.orm.entity_manager"]` – Bart Apr 18 '14 at 14:07
0

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Wcool
  • 329
  • 1
  • 2
  • 9
  • Missing argument 1 for FW\CoreBundle\Form\Type\NationaliteitidType::__construct() I think the class is not found, but why ? – Bart Apr 18 '14 at 13:49
  • 1
    Wanted to add the answer of @mneute, but he beat me to it :) I forgot to ask what version of Symfony you're using. Newer ones indeed don't allow naming them. Have never tried this, but if you're using multiple arguments, I think they're now separated by the comma (in your class) in the same order as defined in services.yml – Wcool Apr 18 '14 at 13:57
0

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);
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
0

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