Question

I'm working on a project where we use DependencyInjection so I have in src\Common\CommonBundle\Resources\config\services.yml the following definition:

services:
    address.form:
        class: Wuelto\Common\CommonBundle\Controller\FormAddressController
        arguments: [@form.factory, @doctrine.orm.entity_manager]
    address_extra_info.form:
        class: Wuelto\Common\CommonBundle\Controller\FormAddressExtraInfoController
        arguments: [@form.factory, @doctrine.orm.entity_manager]

And src\Company\RegisterCompanyBundle\Resources\config\services.yml

services:
    registercompany.form:
        class: Wuelto\Company\RegisterCompanyBundle\Controller\FormRegisterCompanyController
        arguments: [@form.factory, @doctrine.orm.entity_manager]

And this is the code behind the controllers (just one the rest is the same with just classes change):

class FormAddressExtraInfoController {

    public function __construct(FormFactoryInterface $formFactory, EntityManager $em) {
        $this->formFactory = $formFactory;
        $this->em = $em;
    }

    private function getEntity($id) {
        $entity = new AddressExtraInfo();

        try {
            if (isset($id)) {
                $entity = $this->em->getRepository("CommonBundle:AddressExtraInfo")->find($id);
            }
        } catch (\Exception $e) {

        }

        return $entity;
    }

    public function getAction($id = null) {
        $entity = $this->getEntity($id);
        $form = $this->formFactory->create(new AddressExtraInfoType($id), $entity, array('method' => 'POST'));
        return array('formAddressExtraInfo' => $form->createView());
    }

}

So here goes the problem. In another controller(\Website\FrontendBundle\Controller\sellerController.php) outside those bundles I'm trying to get the $formXXX view by using this piece of code:

$this->render('FrontendBundle:Seller:newSellerLayout.html.twig', array($this->get('registercompany.form')->getAction(), $this->get('address_extra_info.form')->getAction()));

But I get this error:

Variable "formCompany" does not exist in FrontendBundle:Seller:newCompany.html.twig at line 10

The reason? I'm not passing values as should be but surprise if I pass them as:

$this->render('FrontendBundle:Seller:newSellerLayout.html.twig', array('formCompany' => $this->get('registercompany.form')->getAction(), 'formAddressExtraInfo' => $this->get('address_extra_info.form')->getAction()));

Then the error transform into this:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, array given

I don't know how to fix this or where I'm doing something wrong?

Était-ce utile?

La solution

Error is clear and meaningful telling you that it needs the FormView instance and you have passed array in your getAction() method i.e return array('formAddressExtraInfo' => $form->createView()); you need to return $form->createView()

public function getAction($id = null) {
    $entity = $this->getEntity($id);
    $form = $this->formFactory->create(new AddressExtraInfoType($id), $entity, array('method' => 'POST'));
    return  $form->createView();
 /*createView() is an instance of Symfony\Component\Form\FormView 
  *which symfony expects while rendering the form
  */
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top