• Please prove you are human by selecting the Plane.

How To Define A Symfony 2 Form Type As A Service

Categories : Article, Symfony

There is no deny that Symfony is a well established and widely used open source web application framework for building enterprise-level applications. Today more ‘n’ more businesses are leveraging from Symfony Development India to get robust and secure web and business applications.

In this blog post, we will see how to define a Symfony 2 form type as a service and call them in your controller. Also, you can easily use that form in other model or service classes outside the controller. The typical way of defining a Symfony 2 form type as a service is:

# src/Acme/TaskBundle/Resources/config/services.yml 
services:acme_demo.form.type.task: class: AcmeTaskBundleFormTypeTaskType tags:- { name: form.type, alias: task }</p>
<p>// src/Acme/TaskBundle/Controller/DefaultController.php<br />// ...public function newAction()<br />{<br /> $task = ...; $form = $this->createForm( 'task', $task ); // ...

Add the following to your services config file in order to use Symfony 2 form factory to create the form in the service container.

If you are using YAML:

# src/Acme/TaskBundle/Resources/config/services.yml
services acme_demo.form.task: factory_method: createNamed factory_service: form.factory<br /> class: SymfonyComponentFormForm<br /> arguments:<br /> - task_form # name of the form
 - task # alias of the form type
 - null # data to bind, this is where your entity could go #if you have that defined as a service
 - { validation_groups: [Default] } # validation groups
 - acme_demo.form.type.task:
 class: AcmeTaskBundleFormTypeTaskType
 - { name: form.type, alias: task }</p>
<p><!-- src/Acme/TaskBundle/Resources/config/services.xml --></p>

 <services> <service id="acme_demo.form.task" factory-method="createNamed" factory-service="form.factory" class="SymfonyComponentFormForm"> <argument>task_form</argument> <!-- name of the form --> <argument>task</argument> <!-- alias of the form type --> <argument>null</argument> <!-- data to bind, this is where your entity could go if you have that defined as a service --> <argument type="collection"> <!-- validation groups --> <argument key="validation_groups">Default</argument> </argument> </service><br /> <service id="acme_demo.form.type.task" class="AcmeTaskBundleFormTypeTaskType"><tag name="form.type" alias="task" /></service></services>

Now you can call your form in the controller

// src/Acme/TaskBundle/Controller/DefaultController.php// ...public function newAction()<br />{ $task = ...; $form = $this->get( 'acme_demo.form.task' );// set initial form data if needed $form->setData( $task );}<br />

You can call the form and inject it in other services wherever you have the service container as you wish or you can do it with a class that handles form submission!

[Total: 0    Average: 0/5]

Client Saying

If you're starting a new business or want to upgrade an existing site, I strongly recommend…
Mr. Manjesh Singh
Daniel Coburn, the president of Bragshare.com gives the honest opinion regarding his experience working with Brainvire…
Mr. Daniel Coburn
Chad Gundry, the CEO of Bragshare.com says that he has worked extensively with Brainvire team since…
Mr. Chad Gundry
The best thing about Brainvire is Compatibility. Whenever we have a query Brainvire is always ready…
Mr Craig Agranoff
Brainvire is a professional company with professional developers who understand the problems that we are facing…
Mr. Clarke Verdel

Latest Happenings

  • 02 Jan 2017
    Brainvire to Go Live at CES 2017 in Vegas! [ + ]
  • 28 Dec 2016
    Brainvire Introduces a Formidable B2B and B2C Retail Commerce Solution – The Retail Omni-Channel ERP [ + ]
Enquiry +