Symfony2未定义变量:handleRequest

时间:2015-01-29 16:10:42

标签: php html symfony yaml

我正在尝试在Symfony2中开发一个基本表单并让handleRequests起作用。但是我遇到了错误undefined variable handleRequest,似乎无法解决问题!

这是我的控制器

 <?php
namespace James\workBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use James\workBundle\Entity\Task;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
    public function newAction(Request $request)
    {    
        $task = new Task();

        $task->setTask('Write a blog post');
        $task->setDueDate(new \DateTime('tomorrow'));

          $form = $this->createFormBuilder($task)
           ->add('task','text')
           ->add('dueDate','date')
           ->add('save','submit')
           ->getForm();

          $form = $handleRequest($request);

         if ($form->isValid())
             {
               exit('form was valid');
               return $this->redirect($this->generateUrl('task_new'));
              }

            //  $request = $this->getRequest(

           //if ($request->getMethod() == 'POST'){
               // $form->bindRequest($request);
                 //if ($form->isValid())
                   //{
               //return $this->redirect($this->generateUrl('task_success'));
          // }
      // }

        return $this->render('JamesworkBundle:Default:index.html.twig', 
              array(
                'form'=> $form->createView(),
                )); 
    }
}

这是我的Twig

 {% extends 'base.html.twig' %}

{% block body %}
  <div class="container">

{{form(form)}}

  </div> 

{% endblock %}

这是我的路由

task_new:
    path:     /
    defaults: { _controller: JamesworkBundle:Default:new  }

task_success:
     path: /success
     defaults: { _controller: JamesworkBundle:Default:success }

1 个答案:

答案 0 :(得分:0)

您错误输入了这一行:

$form = $handleRequest($request);

handleRequest$form处理POST请求的方法。

应该是:

$form->handleRequest($request);