FoS5 - Twig - 如何更改 Twig Form 的输入属性_名称

FoS5 - Twig - How to change input attr_ name of Twig Form

我使用 symfony 5 和 Twig,我用 twig 创建了一个登录表单,但问题是它们的输入 attr_ 名称。我有一些 form[username](例如),但我想要 _username。我该如何更改?

我有:

<input type='text' name='login[username]'> FROM {{ form_widget(login.username) }}

我要:

<input type='text' name='_username'>

非常感谢:D

B.

您可以直接更改表单名称(在 twig 视图中或在 LoginType 中,具体取决于使用的方法)并在您的 Authentificator 中检索相应的名称(要创建或扩展的文件):

public function getCredentials(Request $request)
{
    $credentials = [
        'email' => $request->request->get('login')['email'], // email, or _email, or username, or _username, etc.
        'password' => $request->request->get('login')['password'],
        'csrf_token' => $request->request->get('login')['_token'],
    ];

    $request->getSession()->set(Security::LAST_USERNAME, $credentials['email']);

    return $credentials;
}

https://symfony.com/doc/current/security/guard_authentication.html