forms - Symfony authentification form does not run
问题描述
I have the form type :
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ->add('email')
->add('username', TextType::class)
->add('password', PasswordType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
The entity User :
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(min=2, minMessage="Nom trop petit")
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(min=4, minMessage="Mot de passe trop petit")
*/
private $password;
/**
* @Assert\EqualTo(propertyPath="password", message="Le mot de passe ne correspond pas")
*/
public $confirm_password;
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @inheritDoc
*/
public function getRoles()
{
return ["ROLE_USER"];
}
/**
* @inheritDoc
*/
public function getSalt()
{
// TODO: Implement getSalt() method.
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
}
The controller :
/**
* @Route("/login", name="login")
*/
public function login()
{
// the identification is done thanks Symfony security configuration
return $this->render("security/login.html.twig", [
"form" => $this->createForm(UserType::class )->createView()
]);
}
The security firewall configuration file :
firewalls:
main:
anonymous: lazy
provider: in_database
form_login:
login_path: security_login
check_path: security_login
logout:
path: security_logout
target: home
And the view :
{% extends 'base.html.twig' %}
{% block title %}Hello SecurityController!{% endblock %}
{% block body %}
<h1>Login</h1>
<form action="{{ path('security_login') }}" method="post">
<div class="form-group">
<input type="text" class="form-control" placeholder="Nom" required name="_username">
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Mot de passe" required name="_password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Connexion</button>
</div>
</form>
{% endblock %}
With this code the identication runs.
Now, I want use the Twig form tags in the Twig file as follows:
{{ form_start(form) }}
{{ form_row(form.username, {"attr": {"placeholder": "Nom", "name": "_username", "required": "required"}, "label": "Nom" }) }}
{{ form_row(form.password, {"attr": {"placeholder": "Mot de passe", "name": "_password", "required": "required"}, "label": "Mot de passe" }) }}
<button type="submit" class="btn btn-success">Valider</button>
{{ form_end(form) }}
But when I use this code, I obtain the error message :
The key "_username" must be a string, "NULL" given.
Normally, my forms run correctly. Why, here, doesn't the form work?
Thank for yours answers.
解决方案
我认为您忘记在控制器中处理您的表单请求,为此您需要为Symfony\Component\HttpFoundation\Request
您的方法注入参数login()
,下面的示例:
public function login(Request $request)
{
$form->handleRequest($request);
if (true === $form->isSubmitted() && true === $form->isValid()) {
// stuff
}
}
同样在防火墙中,请编辑form_login
,路由路径不正确,因为在您指定的控制器注释中name="login"
:
form_login:
login_path: login
check_path: login
推荐阅读
- node.js - 如何使用 bot builder sdk v4 中的中间件区分 Bot 到用户和用户到 Bot 消息?
- java - SQLite - 插入新行时表不更新
- azure-devops - 将 Azure Devops 组织迁移到另一个企业
- batch-file - 使用 /f delims 获取子字符串并仍然保留所有分隔符
- javascript - 如何使用对象数组作为基于属性名称的查找表
- graphql - 如何将数组输入到 GraphQL Mutation
- datatables - 过滤后对 DataTables 中的隐藏列求和
- sed - 仅当子字符串的长度超过 3 个字符时,如何将子字符串的第一个字符大写
- html - 使用 EBR 显示 YAML 变量以显示在网站上
- tensorflow - 在初始图像分类器中更改 label_img.py 以接受 numpy 数组(opencv 图像)而不是 jpg 文件