Add better validation for user registration
This commit is contained in:
parent
d4f6b359d5
commit
42f7eff85c
@ -4,6 +4,7 @@ namespace App\Controller;
|
|||||||
|
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Form\RegistrationFormType;
|
use App\Form\RegistrationFormType;
|
||||||
|
use App\Repository\UserRepository;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
@ -17,7 +18,7 @@ class RegistrationController extends AbstractController
|
|||||||
public function register(
|
public function register(
|
||||||
Request $request,
|
Request $request,
|
||||||
UserPasswordHasherInterface $userPasswordHasher,
|
UserPasswordHasherInterface $userPasswordHasher,
|
||||||
EntityManagerInterface $entityManager
|
EntityManagerInterface $entityManager,
|
||||||
): Response
|
): Response
|
||||||
{
|
{
|
||||||
$user = new User();
|
$user = new User();
|
||||||
@ -25,6 +26,10 @@ class RegistrationController extends AbstractController
|
|||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
// Check for duplicate username/email
|
||||||
|
if ($form->get('plainPassword')->getData() !== $form->get('plainPasswordRepeated')->getData()) {
|
||||||
|
$this->addFlash('error', 'Password and password repeated must be the same');
|
||||||
|
} else {
|
||||||
// encode the plain password
|
// encode the plain password
|
||||||
$user->setPassword(
|
$user->setPassword(
|
||||||
$userPasswordHasher->hashPassword(
|
$userPasswordHasher->hashPassword(
|
||||||
@ -37,9 +42,13 @@ class RegistrationController extends AbstractController
|
|||||||
$entityManager->flush();
|
$entityManager->flush();
|
||||||
// do anything else you need here, like send an email
|
// do anything else you need here, like send an email
|
||||||
|
|
||||||
|
$this->addFlash('success', sprintf('Successfully registered user %s', $user->getUsername()));
|
||||||
|
|
||||||
return $this->redirectToRoute('app_test_test1');
|
return $this->redirectToRoute('app_test_test1');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return $this->render('security/register.html.twig', [
|
return $this->render('security/register.html.twig', [
|
||||||
'registrationForm' => $form->createView(),
|
'registrationForm' => $form->createView(),
|
||||||
]);
|
]);
|
||||||
|
@ -8,9 +8,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
|||||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
|
|
||||||
/**
|
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
|
||||||
* @UniqueEntity(fields={"username"}, message="There is already an account with this username")
|
|
||||||
*/
|
|
||||||
#[ORM\Entity(repositoryClass: UserRepository::class)]
|
#[ORM\Entity(repositoryClass: UserRepository::class)]
|
||||||
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||||||
{
|
{
|
||||||
|
@ -24,6 +24,7 @@ class RegistrationFormType extends AbstractType
|
|||||||
// this is read and encoded in the controller
|
// this is read and encoded in the controller
|
||||||
'mapped' => false,
|
'mapped' => false,
|
||||||
'attr' => ['autocomplete' => 'new-password'],
|
'attr' => ['autocomplete' => 'new-password'],
|
||||||
|
'label' => 'Password',
|
||||||
'constraints' => [
|
'constraints' => [
|
||||||
new NotBlank([
|
new NotBlank([
|
||||||
'message' => 'Please enter a password',
|
'message' => 'Please enter a password',
|
||||||
@ -36,6 +37,10 @@ class RegistrationFormType extends AbstractType
|
|||||||
]),
|
]),
|
||||||
],
|
],
|
||||||
])
|
])
|
||||||
|
->add('plainPasswordRepeated', PasswordType::class, [
|
||||||
|
'mapped' => false,
|
||||||
|
'label' => 'Password repeated',
|
||||||
|
])
|
||||||
->add('agreeTerms', CheckboxType::class, [
|
->add('agreeTerms', CheckboxType::class, [
|
||||||
'mapped' => false,
|
'mapped' => false,
|
||||||
'constraints' => [
|
'constraints' => [
|
||||||
@ -44,8 +49,7 @@ class RegistrationFormType extends AbstractType
|
|||||||
]),
|
]),
|
||||||
],
|
],
|
||||||
])
|
])
|
||||||
->add('register', SubmitType::class)
|
->add('register', SubmitType::class);
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function configureOptions(OptionsResolver $resolver): void
|
public function configureOptions(OptionsResolver $resolver): void
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{% extends 'base/base.html.twig' %}
|
{% extends 'base/base.html.twig' %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<h1>Register</h1>
|
<h1 class="h3 mb-3 font-weight-normal">Register</h1>
|
||||||
|
|
||||||
{{ form(registrationForm) }}
|
{{ form(registrationForm) }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in New Issue
Block a user