gpt4 book ai didi

forms - key "_username"必须是一个字符串, "NULL"给定 symfony 4

转载 作者:行者123 更新时间:2023-12-04 08:50:10 32 4
gpt4 key购买 nike

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

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



这是我的 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,));
}

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

我的安全.yaml:
    form_login:
login_path: connexion
check_path: connexion

在此先感谢您的帮助。

最佳答案

form_login 有这两个选项

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

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

关于forms - key "_username"必须是一个字符串, "NULL"给定 symfony 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49056138/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com