ECOMMERCEMagento DevelopmentMagento 2 MigrationOdoo ERPMobile CommerceCRM for CommerceLAMP TECHNOLOGYPHP Website DevelopmentLaravel DevelopmentWordPress DevelopmentSymfony DevelopmentProgressive Web AppsMOBILEiPhone App DevelopmentAndroid App DevelopmentHybrid App DevelopmentIonic DevelopmentXamarin App DevelopmentNODE.JS & MONGODBFull Stack DevelopmentMEAN StackAngularJS & ReactJSNode.JS DevelopmentReact Native DevelopmentMICROSOFTASP.NET DevelopmentAzure ServicesSharePoint DevelopmentCMSPowerBI DevelopmentCLOUD SERVICESDevOpsAmazon Web ServicesAzure Cloud ServiceGoogle App Engine ServicesENTERPRISE SERVICESAugmented RealityCRMEnterprise Mobility ServicesBlockchain ServicesBusiness IntelligenceDIGITAL MARKETINGSearch Engine OptimizationPaid AdvertisingSocial Media MarketingSEO for eCommerceContent MarketingRetailReal EstateEducationFinanceHealthcareMedia & EntertainmentLogisticsAdvertisingDiamond and JewelryAutomotiveMobile and WirelessCloud ServicesGamingIOTBlockchainRetail Omni Channel ERPAuro CRMMulti Vendor MarketplaceBvPOS SystemFacebook BotManaged Analytics- EzlytixDiamond ERPUniversity Management- MantisDigital Asset Profanity- eUnagiHRMSCash in TransitALM with TFSABOUT USFactsheetAbout BrainvireHistoryExecutive LeadershipCore ValuesLife @ BrainvireOur ClientsClient TestimonialsClient’s Video TestimonialsClutch Testimonialshttps://www.brainvire.com/wp/wp-content/uploads/2020/09/Brainvire-Clutch-2020.pnghttps://www.brainvire.com/wp/wp-content/uploads/2020/08/top-software-developer-bv-goodfirms.pnghttps://www.brainvire.com/wp/wp-content/uploads/2020/08/Forbes.pnghttps://www.brainvire.com/wp/wp-content/uploads/2020/08/Daily-herald.png
Insights

How To Define A Symfony 2 Form Type As A Service

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 }

// src/Acme/TaskBundle/Controller/DefaultController.php
// ...public function newAction()
{
$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
 class: SymfonyComponentFormForm
 arguments:
 - 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
tags:
 - { name: form.type, alias: task }

   task_form  task  null    Default  

Now you can call your form in the controller

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

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!

Banner
In search for strategic sessions?.
Let us understand your business thoroughly and help you strategies your digital product..
Book a session
Rate Us!
0/5 (0)