【发布时间】:2014-05-15 15:16:17
【问题描述】:
Symfony 的新手。我创建了一个提交表单,我希望它重定向到 1) 提交成功页面,该页面显示用户刚刚提交的内容,以及 2) 如果字段输入不正确,则会出现错误页面。但是,当我输入字段并提交时,它只会将我带到带有空字段的同一页面。
我有我的控制器:
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$newperson = new Person();
$personform = $this->createForm(new PersonType(), $newperson);
if($this->getRequest()->getMethod() == 'POST') {
$personform->bind($this->getRequest());
if($personform->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($newperson);
$em->flush();
return $this->redirect($this->generateUrl('cir_submitsuccess'));
}
}
return $this->render('CIRBundle:Default:index.html.twig', array(
'personform' => $personform->createView()
));
}
我的路由.yml:
cir_submitsuccess:
pattern: /submit
defaults: { _controller: CIRBundle:Validation:index }
还有我的 index.html.twig:
{% extends '::base.html.twig' %}
{% block body %}
{% if person is defined %}
<ul>
{% for person in person %}
<li>{{person.firstname}} {{person.lastname}} who is {{person.age}} years old and works as a {{person.position}} living in {{person.city}}
{% endfor %}
</ul>
{%endif%}
<br/>
{% endblock %}
验证控制器:
class ValidationController extends Controller
{
public function indexAction() {
return $this->render('CIRBundle:Validatiion:index.html.twig');
}
}
【问题讨论】:
-
你能贴出
cir_submitsuccess的路线代码吗? -
@Alberto Fernández 我做了,你的意思是 routing.yml 正确吗?
-
我说的是
Validation控制器。 -
好的,刚刚编辑完毕。
标签: forms symfony redirect submit twig