user3746259
user3746259

Reputation: 1581

Inject form into Service - in Symfony

I'd like to inject a form (FormInterface) into a service, in my case, it's a form build from "RegisterType". I know I could inject the formfactory and abstract form separate and then create the forminterface within the service, but doing it at once within services injection would be far nicer:

# registration
app.form.type.register:
    class: UserBundle\Form\Type\RegisterType
    tags:
        - { name: form.type, alias: app_user_register }

app.form.handler.register:
    class: UserBundle\Form\Handler\RegistrationFormHandler
    arguments: [@=service('form.factory').create('@app.form.type.register')]
    scope: request

And the RegistrationFormHandler Service - constructor:

public function __construct(FormInterface $form)
{
    $this->form = $form;
}

So I tried by using Symfony Expression languages within the "arguments" for the first time, but I am getting the error message:

"Could not load type '@app.form.type.register'"

How to use the expression language here correct to inject the built form into the constructor? Regards.

Upvotes: 1

Views: 783

Answers (1)

takeit
takeit

Reputation: 4081

I think what you are trying to achieve is:

app.form.handler.register:
    class: UserBundle\Form\Handler\RegistrationFormHandler
    arguments: [@=service('form.factory').create(service('app.form.type.register'))]
    scope: request

Upvotes: 1

Related Questions