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
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertEmail()
     */
    private $email;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertLength(min=2, minMessage="Nom trop petit")
     */
    private $username;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertLength(min=4, minMessage="Mot de passe trop petit")
     */
    private $password;

    /**
     * @AssertEqualTo(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.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *