8000 FormType dependency injection · Issue #17592 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
tamasnyulas opened this issue Jan 28, 2016 · 11 comments
Closed

FormType dependency injection #17592

tamasnyulas opened this issue Jan 28, 2016 · 11 comments

Comments

@tamasnyulas
Copy link

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 the FormFactory::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 with get_class($formType), but client's still able to provide their own dependencies without registering services.

@tina-junold
Copy link

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.

@Tobion
Copy link
Contributor
Tobion commented Jan 28, 2016

I'd like to suggest to let the clients use an instance of a FormTypeInterface as the first parameter in the FormFactory::create() method

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

@Tobion Tobion closed this as completed Jan 28, 2016
@stof
Copy link
Member
stof commented Jan 28, 2016

If you don't use the symfony DI container, you can just implement your own FormExtension that returns a given type for a name

Or just use the existing PreloadedExtension (which is not lazy-loading the types, but might be enough for your use cases)

@tina-junold
Copy link

@Tobion what's with the typeTestCase?

@tamasnyulas
Copy link
Author

@Tobion As I know correctly the reason to remove that was to get rid of the getName() method: #15079
Could you explain why is it not allowed to use our own form type instance for the factory? Was it a performance problem that is caused by the misusage by it's clients? If that's the case is there a chance to have another FormFactory method similar to create() with FormTypeInterface as first parameter to allow the uncached form creation with the benefits of owning the instance of the type?

@Tobion
Copy link
Contributor
Tobion commented Jan 28, 2016

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.

@tina-junold
Copy link

@stof @Tobion any suggestion (or solution) for the TypeTestCase not resolving for forms registered as service?

@stof
Copy link
Member
stof commented Jan 29, 2016

@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).

@tamasnyulas
Copy link
Author

Thanks @stof, I think I got the idea now. Probably I'll need to implement and register my own FormExtensionInterface to solve the problem.

@stof
Copy link
Member
stof commented Jan 29, 2016

@tamasnyulas when using the fullstack framework, there is no need to defined your own FormExtensionInterface. Just define these form types as services with the form.type tag, and the form extension available in the core will handle them.

@sunshinedaniel
Copy link

You could overwrite the function getType() of FormIntegrationTestCase and put you initiated Type in there:

protected function getTypes()
    {
        $transformer = $this->getMockBuilder(Transformer::class)->getMock();
        return [new AddressRequestType($transformer)];
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0