Catch the error in Registration Controller











up vote
0
down vote

favorite












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 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 at 14:24

















up vote
0
down vote

favorite












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 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 at 14:24















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 at 14:25

























asked Nov 13 at 5:11









whitebear

2,811114190




2,811114190








  • 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 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 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 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 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 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 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 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 at 14:24














1 Answer
1






active

oldest

votes

















up vote
0
down vote













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',
    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








    up vote
    0
    down vote













    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

























      up vote
      0
      down vote













      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























        up vote
        0
        down vote










        up vote
        0
        down vote









        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 at 9:08









        del

        11




        11






























             

            draft saved


            draft discarded



















































             


            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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents