0

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)
        );
    }
}
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • 1
    It seems you need to look for validation errors if `$form->isValid()` is `false`. Try to debug, what is in `$form->getErrors()`. But your form should bring it up to inputs if some errors occurred. You can get error messages [like this](https://stackoverflow.com/questions/6978723/symfony2-how-to-get-form-validation-errors-after-binding-the-request-to-the-fo) – derkien Nov 13 '18 at 08:04
  • Im my case somehow `form->getErrors()` return nothing `else if ($form->isSubmitted()) { print $form->getErrors();exit; //nothing returns here. } `, I updated the code – whitebear Nov 13 '18 at 14:24

1 Answers1

0

try replace form_row with form_widget or add form_error in twig template

for more see 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) }}
del
  • 11
  • 2