未定义的方法Symfony \ Component \ Form \ FormBuilder :: createView()

时间:2014-08-19 05:48:48

标签: forms symfony symfony-forms symfony-2.3

我收到错误:

Call to undefined method Symfony\Component\Form\FormBuilder::createView() 

以下代码

public function searchAction(Request $request, $type) {

    $filters = new Filters();

    $form = $this->createFormBuilder($filters)
        ->setAction($this->generateUrl('project_search'))
        ->add('name', 'text');

    if($type == 'extra') {
        $form->add('job', 'text');
    }


    $form->add('send', 'submit', array('label' => "Envoyer"))
        ->getForm();


    return $this->render('Bundle:Controller:search.html.twig', array(
        'form' => $form->createView(),
    ));
}

我相信这是因为我添加了新的输入$form->add('job', 'text');。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

您需要引用Form个实例,而不是FormBuilder。您已经使用了返回所需参考的getForm()方法,但您并未将其分配到任何地方。

$filters = new Filters();

$formBuilder = $this->createFormBuilder($filters)
    ->setAction($this->generateUrl('project_search'))
    ->add('name', 'text');

if($type == 'extra') {
    $formBuilder->add('job', 'text');
}


$formBuilder->add('send', 'submit', array('label' => "Envoyer"));
$form = $formBuilder->getForm();


return $this->render('Bundle:Controller:search.html.twig', array(
    'form' => $form->createView(),
));