在注册控制器中捕获错误

时间:2018-11-13 05:11:41

标签: symfony symfony4

我根据这份文件制作了注册表

How to Implement a Simple Registration Form

它运行良好,但是在出现错误的情况下,它不会返回任何内容,只是重新加载。

例如。

+使用已经存在的用户名。

+密码仔细检查不正确。

在这些情况下,我想显示消息。

如何捕获错误消息???

顺便说一句,如登录

我可以在Security Controller中捕获这样的错误

class SecurityController extends AbstractController
{
    /**
     * @Route("/login", name="app_login")
     */
    public function login(AuthenticationUtils $authenticationUtils): Response
    {

        // get the login error here
        $this->data['error'] = $authenticationUtils->getLastAuthenticationError();

然后我也想在注册控制器中捕获错误

<?php

namespace App\Controller;

use App\Form\UserType;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Service\CommonFunc;

class RegistrationController extends AbstractController
{
    /**
     * @Route("/register", name="user_registration")
     */
    public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder,CommonFunc $commonFunc)
    {

        $this->data['user'] = $this->getUser();
        // 1) build the form
        $user = new User();
        $form = $this->createForm(UserType::class, $user);
        $this->data['error'] = $authenticationUtils->getLastAuthenticationError();
        // 2) handle the submit (will only happen on POST)
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {

            // 3) Encode the password (you could also do this via Doctrine listener)
            $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($password);

            // 4) save the User!
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

            // ... do any other work - like sending them an email, etc
            // maybe set a "flash" success message for the user

            return $this->redirectToRoute('index');
        }else if ($form->isSubmitted()) {  
           print $form->getErrors();exit; //nothing returns here.
        } 


        $this->data = array_merge($this->data,$commonFunc->makeMenuBar());
        $this->data['noLoginForm'] = true;
        return $this->render(
            'registration/register.html.twig',
            array('form' => $form->createView(),'data' => $this->data)
        );
    }
}

1 个答案:

答案 0 :(得分:0)

尝试用form_widget替换form_row或在树枝模板中添加form_error

有关更多信息,请参见Twig Template Form Function and Variable Reference (Symfony Docs)

{# templates/registration/register.html.twig #}

{{ form_start(form) }}
    {{ form_widget(form.username) }}
    {{ form_widget(form.email) }}
    {{ form_widget(form.plainPassword.first) }}
    {{ form_widget(form.plainPassword.second) }}

    <button type="submit">Register!</button>
{{ form_end(form) }}