'Symfony - make:form is not submit
I'm trying to create a registration form with a FormType here RegisterFormType, here is my signup function
#[Route('/inscription', name: 'app_signup', methods: ['GET|POST'])]
public function signup(Request $request, EntityManagerInterface $em): Response
{
$user = new User();
$form = $this->createForm(RegisterFormType::class, $user);
$form->handleRequest($request);
dd($form->isSubmitted() && $form->isValid()); // return FALSE
if ($form->isSubmitted() && $form->isValid()){
$user->setRoles(['ROLE_USER']);
$em->persist($user);
$em->flush();
return $this->redirectToRoute('user_profile');
}
return $this->render('security/signup.html.twig', [
'form' => $form->createView()
]);
}
Here is my RegisterFormType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('firstname', TextType::class, [
'constraints' => [
new Assert\Length(min: 2, minMessage: 'Le prénom doit avoir au minimum 2 caractères')
],
'attr' => [
'class' => 'form-control'
],
'label' => 'Prénom'
])
->add('username', TextType::class, [
'constraints' => [
new Assert\Length(min: 3, minMessage: "Le nom d'utilisateur doit avoir au minimum 3 caractères")
],
'attr' => [
'class' => 'form-control'
],
'label' => 'Nom d\'utilisateur'
])
->add('email', EmailType::class, [
'constraints' => [
new Assert\Email()
],
'attr' => [
'class' => 'form-control'
]
])
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'options' => [
'attr' => [
'class' => 'form-control'
]
],
'first_options' => [
'label' => 'Mot de passe',
],
'second_options' => [
'label' => 'Mot de passe (confirmation)'
],
'invalid_message' => 'Les mots de passe ne correspondent pas'
])
;
}
And finnally, here is my view
{% block body %}
<div class="container">
{{ form_start(form) }}
<div class="form-row w-75 mx-auto p-4">
<div class="form-group col-md-6 mt-2 mx-auto">
{{ form_row(form.firstname) }}
</div>
<div class="form-group col-md-6 mt-2 mx-auto">
{{ form_row(form.username) }}
</div>
<div class="form-group col-md-6 mt-2 mx-auto">
{{ form_row(form.email) }}
</div>
<div class="form-group col-md-6 mt-2 mx-auto">
{{ form_row(form.plainPassword.first) }}
</div>
<div class="form-group col-md-6 mt-2 mx-auto">
{{ form_row(form.plainPassword.second) }}
</div>
<div class="form-group col-md-6 text-center p-2 mt-2 mx-auto">
<input type="submit" class="btn btn-primary w-50" value="S'inscrire"></button>
<span class="small d-block"><a href="{{ path('app_login') }}" class="base-link text-success">Déjà inscrit ?</a></span>
</div>
</div>
{{ form_end(form) }}
</div>
{% endblock %}
But for some reasons I ignore, the submission of the form is not completed, but if I replace plainPassword for password field its working fine. Can someone tell me why please ? Here is my EntityListener for plainPassword field
class UserListener
{
private UserPasswordHasherInterface $hasher;
public function __construct(UserPasswordHasherInterface $hasher)
{
$this->hasher = $hasher;
}
public function prePersist(User $user)
{
$this->hashPassword($user);
}
public function preUpdate(User $user)
{
$this->hashPassword($user);
}
private function hashPassword(User $user)
{
if (!is_null($user->getPlainPassword())){
$hashPassword = $this->hasher->hashPassword($user, $user->getPlainPassword());
$user->setPassword($hashPassword);
$user->setPlainPassword(null);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
