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)
);
}
}
symfony symfony4
add a comment |
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)
);
}
}
symfony symfony4
1
It seems you need to look for validation errors if$form->isValid()
isfalse
. 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 somehowform->getErrors()
return nothingelse if ($form->isSubmitted()) { print $form->getErrors();exit; //nothing returns here. }
, I updated the code
– whitebear
Nov 13 at 14:24
add a comment |
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)
);
}
}
symfony symfony4
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
symfony symfony4
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()
isfalse
. 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 somehowform->getErrors()
return nothingelse if ($form->isSubmitted()) { print $form->getErrors();exit; //nothing returns here. }
, I updated the code
– whitebear
Nov 13 at 14:24
add a comment |
1
It seems you need to look for validation errors if$form->isValid()
isfalse
. 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 somehowform->getErrors()
return nothingelse 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
add a comment |
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) }}
add a comment |
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) }}
add a comment |
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) }}
add a comment |
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) }}
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) }}
answered Nov 13 at 9:08
del
11
11
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
It seems you need to look for validation errors if
$form->isValid()
isfalse
. 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 nothingelse if ($form->isSubmitted()) { print $form->getErrors();exit; //nothing returns here. }
, I updated the code– whitebear
Nov 13 at 14:24