my project symfony 4 I have a function that serves me for authentication but also for registration:
/**
* Login
*
* @Route("/login", name="account_login")
*
* @return Response
*/
public function login2(AuthenticationUtils $utils, Request $request, ObjectManager $manager, UserPasswordEncoderInterface $encoder)
{
// Connexion
$error = $utils->getLastAuthenticationError();
$username = $utils->getLastUsername();
// Inscription
$user = new User();
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$password = $encoder->encodePassword($user, $user->getPassword());
$user->setHash($password);
$manager->persist($user);
$manager->flush();
$this->addFlash(
'success',
'Votre compte a bien été créé ! Vous pouvez maintenant vous connecter'
);
$this->redirectToRoute('account_login');
}
return $this->render('security/login.html.twig', [
'hasError' => $error !== null,
'username' => $username,
'form' => $form->createView(),
]);
}
So in my twig file I have my login form:
<form id="login-form" action="{{path('account_login')}}" method="post" role="form" style="display: block;">
<div class="form-group">
<label for="username">Email</label>
<input type="text" name="_username" id="username" tabindex="1" class="form-control" placeholder="Adresse email" required value={{username}}>
</div>
<div class="form-group">
<label for="password">Mot de passe</label>
<input type="password" name="_password" id="password" tabindex="2" class="form-control" placeholder="Mot de passe">
</div>
</form>
And my registration form :
{{form_start(form)}}
{{form_widget(form)}}
{{form_end(form)}}
But when I submit my registration form, I've this error :
The key "_username" must be a string, "NULL" given.
In my security.yml I've :
form_login:
login_path: account_login
check_path: account_login
default_target_path: absence_index
logout:
path: account_logout
target: account_login
I found this post : The key "_username" must be a string, "NULL" given symfony 4
It's the same problem I think, but I don't understand what's the solution.