how can I call a form (which is defined as a service) from the controller
//Here my code in the controller befor I define Form_NameType() as a service
$form = $this->createForm(new Form_NameType(), $obect_that_hydrate_my_form);
.
// SERVICE
blogbundle.Form_Nameform:
class: BlogBundle\Form\Form_NameType
arguments: [@security.context]
tags:
- {name: form.type, alias: FormAsAService }
To call any service in Symfony
you need to use get
method of ContainerInterface
. So in Controller
class you should call:
$form = $this->container->get('blogbundle.Form_Nameform');
If your Controller
extends Symfony\Bundle\FrameworkBundle\Controller\Controller
then there is an alias for that method, and you can use this syntax (which is shorter):
$form = $this->get('blogbundle.Form_Nameform');
Check documentation of how to use services.
I just found how to do it... I tried before but I mispelled it so could not work.
$form = $this->createForm( 'FormAsAService' , $obect_that_hydrate_my_form);
FormAsAService is the name define in the service as an Alias and should be the same as the name define in the form class in the getName() function.
public function getName(){ return 'FormAsAService';}