Php 将多个表单传递给Symfony2中不同服务中定义的视图
我正在从事一个使用Php 将多个表单传递给Symfony2中不同服务中定义的视图,php,symfony,dependency-injection,symfony-forms,symfony-2.4,Php,Symfony,Dependency Injection,Symfony Forms,Symfony 2.4,我正在从事一个使用DependencyInjection的项目,因此我在src\Common\CommonBundle\Resources\config\services.yml中有以下定义: services: address.form: class: Wuelto\Common\CommonBundle\Controller\FormAddressController arguments: [@form.factory, @doctrine.orm.e
DependencyInjection
的项目,因此我在src\Common\CommonBundle\Resources\config\services.yml
中有以下定义:
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]
和src\companybundle\Resources\config\services.yml
services:
registercompany.form:
class: Wuelto\Company\RegisterCompanyBundle\Controller\FormRegisterCompanyController
arguments: [@form.factory, @doctrine.orm.entity_manager]
这是控制器背后的代码(只有一个,其余的都是相同的,只是类发生了变化):
问题来了。在这些捆绑包之外的另一个控制器(\Website\FrontendBundle\controller\sellerController.php
)中,我试图使用以下代码获取$formXXX
视图:
$this->render('FrontendBundle:Seller:newSellerLayout.html.twig', array($this->get('registercompany.form')->getAction(), $this->get('address_extra_info.form')->getAction()));
但我得到了这个错误:
中不存在变量“formCompany”
FrontendBundle:卖家:第10行的newCompany.html.twig
原因是什么?我传递的不是应该传递的值,但如果我将它们传递为:
$this->render('FrontendBundle:Seller:newSellerLayout.html.twig', array('formCompany' => $this->get('registercompany.form')->getAction(), 'formAddressExtraInfo' => $this->get('address_extra_info.form')->getAction()));
然后将错误转换为:
ContextErrorException:可捕获致命错误:参数1传递给
Symfony\Component\Form\FormRenderer::renderBlock()必须是实例
Symfony\Component\Form\FormView的名称,给定数组
我不知道如何解决这个问题,也不知道我哪里做错了?错误很清楚,告诉您它需要FormView实例,并且您已经在
getAction()
方法中传递了数组,即返回数组('formAddressExtraInfo'=>$form->createView())
您需要返回$form->createView()
$this->render('FrontendBundle:Seller:newSellerLayout.html.twig', array('formCompany' => $this->get('registercompany.form')->getAction(), 'formAddressExtraInfo' => $this->get('address_extra_info.form')->getAction()));
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
*/
}