3

I'm trying to show an user oriented choice list in a form and I don't manage to access to the container to get the current User.

I don't see how to get it in the Repository neither than in the Type.

Any Idea?

Julien Ducro
  • 854
  • 1
  • 9
  • 26

2 Answers2

12

Let say you created a FormType class. You don't know how to pass the container in this object.

Create now your own type extended from FormType and pass the container through the constructor

class MyType extends FormType
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }
}

In your config.yml, define your new type

mytype:
  class: ...\MyType
  arguments: ["@service_container"]
  tags:
      - { name: form.type }

Now, use MyType instead of FormType in all your controllers

Reuven
  • 3,336
  • 2
  • 23
  • 29
  • 1
    How would be the classes of the forms that extend from MyType? I cannot make them to work. I got: "Argument 1 passed to Makrosoft\UtilesBundle\Utiles\MyType::__construct() must be an instance of Symfony\Component\DependencyInjection\ContainerInterface, none given,..." – Rowinson Gallego Jul 31 '12 at 22:22
1

Perhaps you can request the User object in the controller, and pass it on to the repository in the constructor?

Dieter
  • 1,690
  • 2
  • 15
  • 28
  • The repository is constructed in the buildform of the Type, and this function is a static call so I can't get it that way. – Julien Ducro Oct 06 '11 at 22:42