1
新的Symfony。我創建了一個提交表單,我希望它重定向到1)提交成功頁面,顯示用戶剛剛提交的內容,以及2)錯誤頁面,如果字段輸入不正確。但是,當我輸入字段並提交時,只需將我帶到空字段的同一頁面。Symfony表單提交重定向到一個頁面,並顯示剛剛輸入的用戶
我有我的控制器:
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()
));
}
我的routing.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'的代碼嗎? –
@AlbertoFernández我做到了,你的意思是routing.yml正確嗎? – Noobtastic
我在說「驗證」控制器。 –