-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
FormType dependency injection #17592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Additionally i face exactly this problem testing such a form type with TypeTestCase. It is impossible, since TypeTestCase can't access the services: http://stackoverflow.com/questions/35048898/how-to-test-a-symfony-form-type-with-constructor A possibility to inject to the form type constructor would be great. |
This is exactly what has been removed in 3.0. If you don't use the symfony DI container, you can just implement your own FormExtension that returns a given type for a name similar to https://github.com/symfony/symfony/blob/master/src/Symfony/Component 8000 /Form/Extension/DependencyInjection/DependencyInjectionExtension.php |
Or just use the existing PreloadedExtension (which is not lazy-loading the types, but might be enough for your use cases) |
@Tobion what's with the typeTestCase? |
@Tobion As I know correctly the reason to remove that was to get rid of the |
The reasons are explained in #5321. And I don't think this will be reverted as there is no need. Again, the correct solution is already explained and just a few lines more than before. |
@tamasnyulas passing a form instance directly was already discouraged in 2.x as it has a performance impact, as it replaces the type in the registry each time (and it is potentially broken in case of passing instances with different dependencies, depending of what the form type does and how the form looks like). |
Thanks @stof, I think I got the idea now. Probably I'll need to implement and register my own |
@tamasnyulas when using the fullstack framework, there is no need to defined your own FormExtensionInterface. Just define these form types as services with the |
You could overwrite the function
|
In Symfony 3 there's no other way to add dependencies to form types, then defining them as services. Those who want to use the form bundle stand alone, have no chance to have some dependency injected to the type, since Symfony itself handles the instantiation inside the form factory.
I'd like to suggest to let the clients use an instance of a
FormTypeInterface
as the first parameter in theFormFactory::create()
method (or an FQCN string as it's now) and internally don't create other instance of the type. This way whenever you need the FQCN you can have it withget_class($formType)
, but client's still able to provide their own dependencies without registering services.The text was updated successfully, but these errors were encountered: