Last Updated: February 25, 2016
·
1.024K
· ajeliuc

Symfony bundle development. Services. FormBuilder.

When we are in controller we use $this->getFormBuilder(array(1,2,3)->add()->add()->getForm(); form of calling form builder.

But when you are developing third party bundle and want to use form builder (for ex. for delete button etc.) you inject form.factory service in your ControllerAware or controller as a service class. So you need to:

/* In your controller/service class */
protected $formFactory;

public function __contstruct(FormFacotoryInterface $formFactory)
{
    $this->formFactory = $formFactory;
}

protected function createDeleteForm($id)
{
    return $this->formFactory->createBuilder('form', array('id' => $id)
        ->setMethod('DELETE')
        ->setAction('my_delete_action_route', array('id' => $id)
        ->add('id', 'hidden')
        ->add('delete', 'submit')
        ->getForm();
}