Symfony FormEvents出错

时间:2017-08-16 11:24:21

标签: symfony symfony-forms

我呼吁形成每条路线,所以不要总是这样做:

$form = $this->get('form.factory')->createNamed(UserType:class);
$form->handleRequest($request);

我是从树枝UserRegister.html.twig打来的:

 {{ render(controller('AppBundle:Security:register')) }}

和树枝:

{% if form.vars['errors'] is not empty %}
    {{ form_errors(form) }}
{% endif %}

{{ form_start(form) }}
{{ form_end(form)

控制它:

 public function registerAction(Request $request)
{
    $form = $this->get('form.factory')->createNamed('register', UserType::class);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($form->getData());
        $em->flush();
        $em->clear();
        return $this->redirectToRoute('app.homepage');
    }
    return $this->render('@site_forms/UserRegister.html.twig', ['form' => $form->createView()]);
}

那么最大的问题是什么? 如果出错,它只会显示表格! 错误应该返回当前页面!

1 个答案:

答案 0 :(得分:0)

似乎你只在!isValid()案例中渲染表格的树枝。也许它来自这里?

您可以使用$this->redirectToRoute($request->get('_route')始终重定向到当前页面。