首页 > 解决方案 > 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.

标签: formssymfonyidentification

解决方案


我认为您忘记在控制器中处理您的表单请求,为此您需要为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

推荐阅读