Register a form as a service in Symfony 2

If you use the Symfony 2 form builder and create your form types as classes then you may already know that you can define those form types as services and call them in your controller. That process is well documented in the The Book, and works very well. I have an alternative solution which is to define the form itself as a service, which allows you to easily use that form in other model or service classes outside of the controller. This is useful if you have a separate class that handles form submission which may need the form as well and you can inject the actual form into that service.

The typical way of defining a form type as a service and using the form factory to create the form in the controller looks like this:

 

To use the Symfony 2 form factory to create the form in the service container, add the following to your services config file.

If you use YAML:

If you use XML:

 

Now you can call your form like this in your controller

 

Wherever you have the service container you can call the form and you can inject the form into other services, as you may want to do with a class that handles form submission.

5 comments on “Define a Symfony 2 Form as a Service

  1. satahippy says:

    beware! with this approach DI will always returns the same Form
    that differs from Controller::createForm

    1. rasta says:

      doesn’t have to, if you define the proper scope

  2. Horecio says:

    Very good.
    I was trying to do so. For I use in ZF2 this way. But I was kind of lost in yaml.
    Thanks and good job.

  3. George Ploscaru says:

    Go to https://github.com/symfony/symfony/issues/17013 aliemre’s comment for Symfony 3

  4. sathi says:

    if i am using a symfony 3.2 version then how can i register form….?

Comments are closed.