symfony: [Form] Cannot use setter injection in FormType ?

I’m using Symfony 3.0

Is it not possible to use setter injection within the method configureOptions on a FormType.

I wanted to inject the router through a setter injection :

services:    
    form_type.router:
        class: AppBundle\Form\RouterType
        calls:
            - [setRouter,["@router"]]
        tags:
            - { name: form.type }

Here I tested it at several positions :

abstract class AbstractRoutableType extends AbstractType
{
    protected $router;

    public function setRouter($router) // Setter injection
    {
        $this->router = $router;
    }

    public function configureOptions(OptionsResolver $resolver)
    {  
        $this->setRouter();      // <--- Tested : Don't work (not in a closure should work)
        $resolver->setDefault('route',false);        
        $resolver->setDefault('attr', function(Options $options, $attr){

            if($options['route']){
                $this->setRouter();      // <--- Where I want to use id  : Don't work (Normal in a closure ?)
                $attr['data-ajax--url']=$this->router->generate($options['route']);
            }

            return $attr;
        });
    }
}

It throws error ‘missing argument 1 for setRouter’ …

Bug or normal behaviour ?

EDIT : Just see that it is the same in a service tagged ‘doctrine.event_listener’

Say me if I should reproduce the bug in a symfony-standard fork.

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@HeahDude you can access $this from within a closure, if its defined within a class instance or bound to one with Closure::bind since php 5.5 (or so)