Symfony 3.4自动装配失效,表单DataTransformer注入报错求助
Hey there, let's tackle this type error you're hitting with your VarianteEscalierOptGcEditPoteauType form and VarianteEscalierTransformer. The core issue here is that Symfony isn't injecting the required transformer instance into your form type when it's created—let's break down the reasons and fixes step by step.
Why This Happens
In Symfony 3.4, if your form type has dependencies (like a DataTransformer) in its constructor, you can't just instantiate it directly with new VarianteEscalierOptGcEditPoteauType() anymore. The framework needs to handle dependency injection for you, which means your form type needs to be registered as a service, and you need to fetch it from the service container instead of creating it manually.
It sounds like you might be either:
- Still using
newto create the form type instead of using the container - Haven't properly registered your form type as a service with the transformer dependency
- Made a mistake in the service configuration that's preventing the injection
Step-by-Step Fixes
1. Register Your DataTransformer as a Service (If Needed)
First, make sure your VarianteEscalierTransformer is set up as a service. In Symfony 3.4, if you're using default autoconfiguration, it might already be registered, but let's confirm with your app/config/services.yml:
services: AppBundle\Form\DataTransformer\VarianteEscalierTransformer: autowire: true autoconfigure: true public: false
2. Register Your Form Type as a Service with Dependency Injection
Next, register your form type and inject the transformer into it. Add this to your services.yml:
services: # ... other existing services AppBundle\Form\VarianteEscalierOptGc\VarianteEscalierOptGcEditPoteauType: autowire: true autoconfigure: true arguments: - '@AppBundle\Form\DataTransformer\VarianteEscalierTransformer' tags: - { name: form.type }
The form.type tag tells Symfony this is a form type service, so it will be recognized automatically when building forms.
3. Update Your Form Type Constructor
Double-check that your form type's constructor correctly accepts the transformer and stores it as a class property:
<?php namespace AppBundle\Form\VarianteEscalierOptGc; use AppBundle\Form\DataTransformer\VarianteEscalierTransformer; use Symfony\Component\Form\AbstractType; class VarianteEscalierOptGcEditPoteauType extends AbstractType { private $transformer; public function __construct(VarianteEscalierTransformer $transformer) { $this->transformer = $transformer; } // ... rest of your form type code (like buildForm where you attach the transformer) }
4. Fetch the Form Type from the Service Container in Your Controller
Instead of creating the form type with new, use the createForm method with the fully qualified class name (Symfony will pull it from the container):
// In your controller action $form = $this->createForm( VarianteEscalierOptGcEditPoteauType::class, $yourEntityInstance, // Optional form options [] );
5. Verify Autoconfiguration Settings
If you're relying on autoconfiguration, make sure your services.yml has the base configuration that auto-registers your bundle's services:
services: _defaults: autowire: true autoconfigure: true public: false AppBundle\: resource: '../../src/AppBundle/*' exclude: '../../src/AppBundle/{DependencyInjection,Entity,Tests}'
This will automatically register form types and inject their dependencies if they're type-hinted in the constructor.
Quick Troubleshooting Checks
- Clear your Symfony cache with
php bin/console cache:clear—old service configurations can stick around unexpectedly - Double-check namespace spelling for both the transformer and form type (typos here are a super common culprit)
- Ensure you haven't accidentally excluded the form type or transformer directories in your service configuration
内容的提问来源于stack exchange,提问作者Preciel

