我想在Symfony2中创建一个表单,因此我遵循了此site上的教程
namespace Project\Foo\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Project\Foo\Entity\Anfrage;
use Symfony\Component\HttpFoundation\Request;
class UploadController extends Controller
{
public function indexAction(Request $request)
{
$anfrage = new Anfrage();
$anfrage->setName('Güntaa');
$anfrage->setAge(5);
$anfrage->setEmail('[email protected]');
$form = $this->createFormBuilder($anfrage)
->add('save', 'submit', array('label' => 'Create Task'))
->getForm();
return $this->render(
'Foo:Upload:index.html.twig',
array(
'title' => 'Foo',
'form' => $form->createView(),
));
}
}
在我的模板中,我想调用此表单:
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
但是,当我调用模板时,出现以下错误:
我不知道如何解决这个问题。
编辑
这是
Anfrage
的实体:<?php
namespace Project\MarkupConverterBundle\Entity;
class Anfrage {
protected $name;
protected $age;
protected $email;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getAge()
{
return $this->age;
}
public function setAge($age)
{
$this->age = $age;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->$email = $email;
}
}
编辑2
当我尝试使用不带类的表格时,出现相同的错误
namespace Project\Foo\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class UploadController extends Controller
{
public function indexAction(Request $request)
{
$defaultData = array('message' => 'The message from you');
$form = $this->createFormBuilder($defaultData)
->add('message', 'text')
->add('save', 'submit', array('label' => 'Create Task'))
->getForm();
$form->handleRequest($request);
if ($form->isValid()){
$data = $form->getData();
}
return $this->render(
'Foo:Upload:index.html.twig',
array(
'title' => 'Foo',
'form' => $form->createView(),
));
}
}
最佳答案
解决的问题:我的服务名称是Validator。这不是一个好主意。感谢所有尝试提供帮助的人。