如何在Symfony2中使用Form类

时间:2012-03-21 10:47:34

标签: php forms class symfony formbuilder

我正在关注如何使用表单类的Symfony2中的the tutorial

我做错了,因为当我使用以下示例代码时:

// src/Acme/TaskBundle/Controller/DefaultController.php

// add this new use statement at the top of the class
use Acme\TaskBundle\Form\Type\TaskType;

public function newAction()
{
    //$task = // ... ???
    $form = $this->createForm(new TaskType(), $task);

// ...
}

...我收到以下错误:

注意:未定义的变量:task

我知道$ task尚未正确定义。任何人都可以向我解释我应该如何定义它?我尝试将其创建为实体,将formType创建为未定义的变量,但一切都没有运气。

干杯

1 个答案:

答案 0 :(得分:4)

如果您按照第一个教程进行操作,那么您应该在Task命名空间中创建一个Acme\TaskBundle\Entity实体。所以你的控制器就是,

// src/Acme/TaskBundle/Controller/DefaultController.php

// add this new use statement at the top of the class
use Acme\TaskBundle\Form\Type\TaskType;
use Acme\TaskBundle\Entity\Task;

public function newAction()
{
    $task = new Task();
    $form = $this->createForm(new TaskType(), $task);

// ...
}