{"action":"created","issue":{"url":"https://api.github.com/repos/symfony/symfony/issues/18231","repository_url":"https://api.github.com/repos/symfony/symfony","labels_url":"https://api.github.com/repos/symfony/symfony/issues/18231/labels{/name}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/18231/comments","events_url":"https://api.github.com/repos/symfony/symfony/issues/18231/events","html_url":"https://github.com/symfony/symfony/issues/18231","id":142050876,"number":18231,"title":"[Form] Cannot use setter injection in FormType ?","user":{"login":"Alsatian67","id":7631072,"avatar_url":"https://avatars.githubusercontent.com/u/7631072?v=3","gravatar_id":"","url":"https://api.github.com/users/Alsatian67","html_url":"https://github.com/Alsatian67","followers_url":"https://api.github.com/users/Alsatian67/followers","following_url":"https://api.github.com/users/Alsatian67/following{/other_user}","gists_url":"https://api.github.com/users/Alsatian67/gists{/gist_id}","starred_url":"https://api.github.com/users/Alsatian67/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Alsatian67/subscriptions","organizations_url":"https://api.github.com/users/Alsatian67/orgs","repos_url":"https://api.github.com/users/Alsatian67/repos","events_url":"https://api.github.com/users/Alsatian67/events{/privacy}","received_events_url":"https://api.github.com/users/Alsatian67/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":13,"created_at":"2016-03-19T10:45:44Z","updated_at":"2016-03-20T09:48:19Z","closed_at":"2016-03-19T13:51:59Z","body":"I'm using Symfony 3.0\r\n\r\nIs it not possible to use setter injection within the method configureOptions on a FormType.\r\n\r\nI wanted to inject the router through a setter injection :\r\n```yaml\r\nservices:    \r\n    form_type.router:\r\n        class: AppBundle\\Form\\RouterType\r\n        calls:\r\n            - [setRouter,[\"@router\"]]\r\n        tags:\r\n            - { name: form.type }\r\n```\r\n\r\nHere I tested it at several positions :\r\n\r\n```php\r\nabstract class AbstractRoutableType extends AbstractType\r\n{\r\n    protected $router;\r\n        \r\n    public function setRouter($router) // Setter injection\r\n    {\r\n        $this->router = $router;\r\n    }\r\n    \r\n    public function configureOptions(OptionsResolver $resolver)\r\n    {  \r\n        $this->setRouter();      // <--- Tested : Don't work (not in a closure should work)\r\n        $resolver->setDefault('route',false);        \r\n        $resolver->setDefault('attr', function(Options $options, $attr){\r\n           \r\n            if($options['route']){\r\n                $this->setRouter();      // <--- Where I want to use id  : Don't work (Normal in a closure ?)\r\n                $attr['data-ajax--url']=$this->router->generate($options['route']);\r\n            }\r\n            \r\n            return $attr;\r\n        });\r\n    }\r\n}\r\n```\r\n\r\nIt throws error 'missing argument 1 for setRouter' ...\r\n\r\nBug or normal behaviour ?\r\n\r\nEDIT : Just see that it is the same in a service tagged 'doctrine.event_listener'\r\n\r\nSay me if I should reproduce the bug in a symfony-standard fork.\r\n\r\nThanks."},"comment":{"url":"https://api.github.com/repos/symfony/symfony/issues/comments/198886412","html_url":"https://github.com/symfony/symfony/issues/18231#issuecomment-198886412","issue_url":"https://api.github.com/repos/symfony/symfony/issues/18231","id":198886412,"user":{"login":"Alsatian67","id":7631072,"avatar_url":"https://avatars.githubusercontent.com/u/7631072?v=3","gravatar_id":"","url":"https://api.github.com/users/Alsatian67","html_url":"https://github.com/Alsatian67","followers_url":"https://api.github.com/users/Alsatian67/followers","following_url":"https://api.github.com/users/Alsatian67/following{/other_user}","gists_url":"https://api.github.com/users/Alsatian67/gists{/gist_id}","starred_url":"https://api.github.com/users/Alsatian67/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Alsatian67/subscriptions","organizations_url":"https://api.github.com/users/Alsatian67/orgs","repos_url":"https://api.github.com/users/Alsatian67/repos","events_url":"https://api.github.com/users/Alsatian67/events{/privacy}","received_events_url":"https://api.github.com/users/Alsatian67/received_events","type":"User","site_admin":false},"created_at":"2016-03-20T09:48:19Z","updated_at":"2016-03-20T09:48:19Z","body":"Thank you for the explaination HeahDude.\r\n\r\nSince years and years I used somthing like :\r\n\r\n```php\r\n    if (null === $this->router) {\r\n        $this->setRouter();\r\n    }\r\n```\r\n\r\nAnd I thought it worked because it was never called.\r\n\r\n\r\nI thought I should call the setter explicitely because of this sentence in the cookbook :\r\n\r\n> Setter injection works well with optional dependencies. If you do not need the dependency, then just **do not call the setter**.\r\n\r\n(So I thought the DI maked some cached class where it adds the dependency argument with the to my method call without argument ...)\r\n\r\nhttp://symfony.com/doc/current/components/dependency_injection/types.html#setter-injection"}}