Symfony/Form: Too few arguments to function DoctrineType::__construct()
Asked Answered
O

5

6

I've got a weird error. I implemented the Form component in my own system. There I created a FormType where I use the EntityType for a field. Everytime i wanna create the form with the formBuilder it throws the following error:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function 
Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct(), 0 passed in 
vendor/symfony/form/FormRegistry.php on line 92 and exactly 1 expected in 
vendor/symfony/doctrine-bridge/Form/Type/DoctrineType.php:102

Does anyone here has an idea why this is happening?

Additional information:

composer package versions:

abraham/twitteroauth          0.7.4
doctrine/annotations          v1.6.0
doctrine/cache                v1.7.1
doctrine/collections          v1.5.0
doctrine/common               v2.8.1
doctrine/dbal                 v2.6.3
doctrine/inflector            v1.3.0
doctrine/instantiator         1.1.0
doctrine/lexer                v1.0.1
doctrine/orm                  v2.6.1
monolog/monolog               1.23.0
php-curl-class/php-curl-class 8.0.1
phpmailer/phpmailer           v6.0.3
psr/cache                     1.0.1
psr/log                       1.0.2
psr/simple-cache              1.0.0
setasign/fpdf                 1.8.1
symfony/cache                 v4.0.5
symfony/console               v4.0.5
symfony/doctrine-bridge       v4.0.5
symfony/event-dispatcher      v4.0.5
symfony/filesystem            v4.0.5
symfony/form                  v4.0.5
symfony/http-foundation       v4.0.5
symfony/inflector             v4.0.5
symfony/intl                  v4.0.5
symfony/options-resolver      v4.0.5
symfony/polyfill-intl-icu     v1.7.0
symfony/polyfill-mbstring     v1.7.0
symfony/polyfill-php72        v1.7.0
symfony/process               v4.0.5
symfony/property-access       v4.0.5
symfony/security-core         v4.0.5
symfony/security-csrf         v4.0.5
symfony/translation           v4.0.5
symfony/twig-bridge           v4.0.5
symfony/validator             v4.0.5 
symfony/var-dumper            v4.0.5
symfony/yaml                  v4.0.5
twig/extensions               v1.5.1
twig/twig                     v2.4.6

Form Type:

public function buildForm( FormBuilderInterface $builder, array $options )
{
    $builder
        ->add( 'name', TextType::class )
        ->add( 'image', TextType::class )
        ->add( 'typeId', IntegerType::class )
        ->add( 'customFields', EntityType::class, [
            'class' => TypeDefinition::class,
            'choice_label' => 'name',
            'multiple' => true
        ] )
        ->add( 'isEnabled', CheckboxType::class )
        ->add( 'count', IntegerType::class );
}

If you need more information please tell me below :)

Oblivious answered 4/3, 2018 at 22:40 Comment(4)
php version? And did this use to be a S3 app in which you manually updated composer.json?Caras
@Caras My local PHP version is 7.1. I use the form component in my own application together with doctrine. It's NOT the Symfony framework which I use.Oblivious
Okay. I'm not seeing the dependency injection component in your list. Some form types need additional constructor arguments. One assumes, for example, that EntityType needs an entity manager instance. In the framework, these sorts of objects would be pulled from the service container. I can't tell you how to do it in your framework.Caras
I found a solution, please consider my recent answer!Brassy
O
8

I finally found the answer to my problem: It's NOT possible to use the EntityType class out of the SymfonyFramework context.

The reason is, that the EntityType::class needs multiple components to get resolved by the FormBuilder of Symfony. Of course you need Doctrine and the doctrine-bridge for Symfony. But for resolving the final entity from the EntityType::class, the DoctrineType:class needs a ManagerRegistry::class to be passed. With the ManagerRegistry the Doctrine connection can be resolved from the Symfony application.

So one possibility is to pass the entity manager or the already resolved entities with the config to the FormType and create a field with the ChoiceType::class type.

I hope this is helpful for some of you...

Oblivious answered 9/3, 2018 at 10:17 Comment(2)
Do you know a way to achieve this in your unit tests?Hearthstone
I found a solution, please consider my recent answer!Brassy
T
3

Just add your form class inside services.yml. this method worked for me

[bundle_name].form.[class_name]:
   class: [bundle_name]\Form\Type\[class_name]
   arguments: ["@doctrine.orm.entity_manager"]
   tags:
     - { name: form.type }

careful with indention tho, cause yml requires proper indention.

Tumor answered 10/5, 2019 at 7:47 Comment(0)
C
0

If you don't use Symfony Framework, enable autoconfigure and autowire in your services.yaml. Example :

App\Form\:
    resource: '../src/Form'
    autowire: true
    autoconfigure: true
Crowfoot answered 5/3, 2018 at 8:32 Comment(1)
Thanks for the help but I already found a solution or better; the answer.Oblivious
R
0

Your form might not be registered properly in the services.yml?

services:
    Namespace\Type\YourType:
        autowire: true
        tags:
            - { name: form.type }

You can also get this exact same error when using EntityType::class while you are actually using Doctrine's MongoDB ODM instead of their EntityManager

Solution for MongoDB is to use DocumentType::class instead of EntityType::class

Ridge answered 6/5, 2021 at 9:43 Comment(0)
B
0

I had exactly the same issue. And found a solution.

It is indeed possible to use EntityType fields outside your project (e.g. in a bundle!)


Here is the solution:

1. Create an EntityType field class inside your bundle

(e.g. in src/Form/Builder/Type/EntityType.php)
Behind the scene, this class will inherit doctrines DoctrineType which (to this day) has a __construct() that requires a RegistryManager object parameter!

There lies your problem - the integrated bundle does not automatically wire the inherited constructors parameters, so you have to do this manually..

2. Add the same constructor from DoctrineType in your EntityType-class:
namespace MyCorp\FormsBundle\Form\Type;

use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class EntityType extends EntityType
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry);
    }
}
3. Manually wire the ManagerRegistry object in your bundle's services.yaml
services:
    MyCorp\FormsBundle\Form\Type\FormEntityType:
        arguments:
            $registry: '@doctrine'

Now you can implement and inherit EntityType from your bundle within any project. Works perfectly!


If it does not work, your bundle's services.yaml may not being loaded correctly! Consider recent updates to symfony 6.2 having a new internal API for DependencyInjections, being available if your bundle main class inherits from AbstractBundle. You then, do not need a spearate BundleExtension class anymore, to ship your yaml configuration files.

In your bundle main class add this:

namespace MyCorp\FormsBundle;

class MyCorpFormsBundle extends AbstractBundle
{
    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        $container->import('../config/services.yaml');
    }
}
Brassy answered 20/12, 2022 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.