Catch the error in Registration Controller










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 AppController;

use AppFormUserType;
use AppEntityUser;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
use AppServiceCommonFunc;

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)
);











share|improve this question



















  • 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

    – derkien
    Nov 13 '18 at 8: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
















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 AppController;

use AppFormUserType;
use AppEntityUser;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
use AppServiceCommonFunc;

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)
);











share|improve this question



















  • 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

    – derkien
    Nov 13 '18 at 8: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














0












0








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 AppController;

use AppFormUserType;
use AppEntityUser;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
use AppServiceCommonFunc;

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)
);











share|improve this question
















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 AppController;

use AppFormUserType;
use AppEntityUser;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
use AppServiceCommonFunc;

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)
);








symfony symfony4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:25







whitebear

















asked Nov 13 '18 at 5:11









whitebearwhitebear

2,821114390




2,821114390







  • 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

    – derkien
    Nov 13 '18 at 8: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





    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

    – derkien
    Nov 13 '18 at 8: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




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

– derkien
Nov 13 '18 at 8:04






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

– derkien
Nov 13 '18 at 8: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






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 Answer
1






active

oldest

votes


















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)







share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274216%2fcatch-the-error-in-registration-controller%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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)







    share|improve this answer



























      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)







      share|improve this answer

























        0












        0








        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)







        share|improve this answer













        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)








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 9:08









        deldel

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274216%2fcatch-the-error-in-registration-controller%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3