skrilled
skrilled

Reputation: 5371

Autowire a form in Symfony 4

Unable to understand

Cannot autowire service "App\Form\SomeFormType": argument "$parameterBag" of method "__construct()" references interface "Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface" but no such service exists. Did you create a class that implements this interface?

If I remove the construct of the form and check debug:autowiring I get this though:

[root@ip-10-0-1-32 pos]# bin/console debug:autowiring | grep Param
  Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface
  Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface

Not sure how to resolve outside of injecting parameters manually in services.yml

Upvotes: -1

Views: 2001

Answers (1)

Karol Gasienica
Karol Gasienica

Reputation: 2924

Do you have configured autowire properly in your config/services.yaml file ?

Most important part:

# config/services.yaml
services:
    _defaults:
        autowire: true // <--- it should be true
        autoconfigure: true
        public: false

You can also make available to autowire single service:

Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface:
    autowire: true

Anyway I think you shouldn't pass whole ParameterBag into your form. You should pass values which you need while creating a form.

I bet you are doing something like:

$form = $this->createForm(App\Form\SomeFormType::class, $data, [
    'key' => 'value', //it should be also some options builder class
]);

You can pass some options in 3rd parameter, and you can access them inside SomeFormType:

class SomeFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        dump($options); // will output parameters from createForm (['key' => 'value'])
    }
// ...

Manual

Defining Services Dependencies Automatically (Autowiring)

Symfony Forms

Upvotes: 1

Related Questions