Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The key "_username" must be a string, "NULL" given symfony 4

Tags:

forms

symfony

I have created a registration form. But when I submit I have the following error message

The key "_username" must be a string, "NULL" given

here is my controller

  public function connexion(Request $request, AuthenticationUtils $authUtils, UserPasswordEncoderInterface $passwordEncoder) {

            // get the login error if there is one
            $error = $authUtils->getLastAuthenticationError();
            // last username entered by the user
            $lastUsername = $authUtils->getLastUsername();

            // 1) build the form
            $user              = new User();
            $form_registration = $this->createForm(RegistrationType::class, $user);

            // 2) handle the submit (will only happen on POST)
            $form_registration->handleRequest($request);
            if ($form_registration->isSubmitted() && $form_registration->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!
                $em = $this->getDoctrine()->getManager();
                $em->persist($user);
                $em->flush();

                // ... do any other work - like sending them an email, etc
                // maybe set a "flash" success message for the user

                return $this->redirectToRoute('connexion');
            }

            return $this->render('connexion.html.twig', array(
                'form_registration' => $form_registration->createView(),
                'last_username' => $lastUsername,
                'error'         => $error,));
        }

my html

{{ form_start(form_registration) }}
{{ form_row(form_registration.username) }}
{{ form_row(form_registration.email) }}
{{ form_row(form_registration.plainPassword.first) }}
{{ form_row(form_registration.plainPassword.second) }}

<button type="submit">Register!</button>
{{ form_end(form_registration) }}

Do you know why I have this error message?

Normally when I fill in my username I should not have it.

I don't have copy paste my RegistrationType but it is ok I have tested it in a previous RegistrationController and it worked perfectly..

here is my Ctrl-u result

<section class="section-main-inscription_rapide">
    <h3>INSCRIPTION GRATUITE</h3>
    <form name="registration" method="post">
    <div><label for="registration_username" class="required">Username</label><input type="text" id="registration_username" name="registration[username]" required="required" /></div>
    <div><label for="registration_email" class="required">Email</label><input type="email" id="registration_email" name="registration[email]" required="required" /></div>
    <div><label for="registration_plainPassword_first" class="required">Password</label><input type="password" id="registration_plainPassword_first" name="registration[plainPassword][first]" required="required" /></div>
    <div><label for="registration_plainPassword_second" class="required">Repeat Password</label><input type="password" id="registration_plainPassword_second" name="registration[plainPassword][second]" required="required" /></div>

    <button type="submit">Register!</button>
    <input type="hidden" id="registration__token" name="registration[_token]" value="xQ6gADeUkUb424-2ccvtyXd_atle7dYCPW0OLTefI_g" /></form>
</section>

My security.yaml:

    form_login:
        login_path: connexion
        check_path: connexion

Thanks in advance for your help.

like image 583
R Lopez Avatar asked Jan 28 '23 13:01

R Lopez


1 Answers

form_login has these 2 options

            username_parameter: "login_form[username]"
            password_parameter: "login_form[password]"

By default these are set to _username and _password, so you either have to change the field names to _username and _password or change the security.yml file username_parameter and password_parameter to your field names.

like image 157
Gabb1995 Avatar answered Jan 31 '23 07:01

Gabb1995