我正在尝试渲染一个我刚从实体生成的表单,但我收到了以下错误...
<?php
namespace Prueba\FrontendBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Prueba\FrontendBundle\Form\ItemType;
class DefaultController extends Controller
{
/**
* @Route("/hello")
* @Template()
*/
public function indexAction($name)
{
$form = new ItemType();var_dump(get_class($form));
return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
'form' => $form->createView(),
));
}
}
string(35)“Prueba \ FrontendBundle \ Form \ ItemType”致命错误:致电 未定义的方法Prueba \ FrontendBundle \ Form \ ItemType :: createView()in /home/javier/programacion/sf2000/src/Prueba/FrontendBundle/Controller/DefaultController.php 第20行
答案 0 :(得分:1)
更改
$form = new ItemType();
到
$form = $this->createForm(new FormType());
如果你想在表单中附加一个空实体(更容易验证和表单处理):
$item = new Item();
$form = $this->createForm(new FormType(), $item);