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

问题描述 投票:3回答:2

我已经创建了一个注册表。但是当我提交时,我有以下错误消息

键“_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,));
        }

我的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) }}

你知道为什么我有这个错误信息吗?

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

我没有复制粘贴我的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>

我的security.yaml:

    form_login:
        login_path: connexion
        check_path: connexion

在此先感谢您的帮助。

forms symfony
2个回答
3
投票

form_login有这两个选项

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

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


2
投票

或者在表单类中,您可以覆盖方法'getBlockPrefix()'并使其返回空字符串。

© www.soinside.com 2019 - 2024. All rights reserved.