Reputation: 141
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?
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 :)
Upvotes: 6
Views: 11414
Reputation: 2073
I had exactly the same issue. And found a solution.
EntityType
fields outside your project (e.g. in a bundle!)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..
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);
}
}
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');
}
}
Upvotes: 0
Reputation: 3406
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
Upvotes: 0
Reputation: 61
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.
Upvotes: 3
Reputation: 141
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...
Upvotes: 8
Reputation: 1468
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
Upvotes: 0