I made registration form according to this document
How to Implement a Simple Registration Form
It works well but, in case of error, it doesn't return anything and just reload.
For example.
+Use already existed username.
+Password double check is not correct.
I want to show message in these cases.
How can I catch the error message ???
BTW,as for login
I can catch the error like this in 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();
then I want to catch the error as well in Registration Controller
<?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)
);
}
}