Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 965
Scrummer Avatar asked Mar 06 '23 18:03

Scrummer


2 Answers

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...

like image 181
Scrummer Avatar answered Mar 14 '23 12:03

Scrummer


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.

like image 31
Mohammad Waeil Tingao Avatar answered Mar 14 '23 10:03

Mohammad Waeil Tingao