密钥"_username"必须为字符串,"NULL"给予symfony 4 [英] The key "_username" must be a string, "NULL" given symfony 4

查看:56
本文介绍了密钥"_username"必须为字符串,"NULL"给予symfony 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了注册表. 但是当我提交时,我收到以下错误消息

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

键"_username"必须是字符串,给定为"NULL"

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

这是我的控制器

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

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

Do you know why I have this error message?

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

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

我没有复制粘贴我的RegistrationType的方法,但是可以,我已经在以前的RegistrationController中对其进行了测试,并且效果很好.

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

这是我的Ctrl-u结果

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>

我的security.yaml:

My security.yaml:

    form_login:
        login_path: connexion
        check_path: connexion

预先感谢您的帮助.

推荐答案

form_login具有这2个选项

form_login has these 2 options

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

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

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.

这篇关于密钥"_username"必须为字符串,"NULL"给予symfony 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆