键“_username”必须是字符串,给定 symfony 4 为“NULL”

R L*_*pez 8 forms symfony

我已经创建了一份注册表。但是当我提交时出现以下错误消息

键“_username”必须是字符串,给定“NULL”

这是我的控制器

  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,));
        }
Run Code Online (Sandbox Code Playgroud)

我的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) }}
Run Code Online (Sandbox Code Playgroud)

你知道为什么我会收到此错误消息吗?

通常,当我填写用户名时,我不应该有它。

我没有复制粘贴我的 RegistrationType,但没关系,我已经在以前的 RegistrationController 中测试了它,它工作得很好。

这是我的 Ctrl-u 结果

<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>
Run Code Online (Sandbox Code Playgroud)

我的 security.yaml:

    form_login:
        login_path: connexion
        check_path: connexion
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。

Gab*_*995 9

form_login 有这 2 个选项

            username_parameter: "login_form[username]"
            password_parameter: "login_form[password]"
Run Code Online (Sandbox Code Playgroud)

默认情况下,它们设置为 _username 和 _password,因此您必须将字段名称更改为 _username 和 _password,或者将 security.yml 文件 username_parameter 和 password_parameter 更改为您的字段名称。