Symfony Form Class: rename fields name attribute's value

岁酱吖の 提交于 2019-12-04 19:54:49

You make things way to complicated. Your form class is useless for the login form. Form classes are a good utility when you work with entities, but you need just to submit two fields and pass them to the login check. So you don't update or create an entity. But this is where you actually need a form class.

Read the doc: http://symfony.com/doc/current/book/forms.html#creating-form-classes


You said you want to learn this but you learn it the wrong way. As they advise you here http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form you should use a simple twig template as login form an no form class.


The logic in the form field actually is, that form is the object and the name in the brackets is the attribute so:

product[description]

is a form field that shows that the form is dealing with the "product" entity and the specific field which is "description" wants you to input a description text.


Use FOSUserBundle: https://github.com/FriendsOfSymfony/FOSUserBundle don't reinvent the wheel ;)

As @mvrhov mentioned you should default parameters of your firewall to match your custom form type, here is greate article that demonstrates this.

You do this by changing your firewall to match this:

security:
firewalls:
    my_firewall:
        pattern:    ^/
        form_login:
            username_parameter: "form_name[username_field_name]"
            password_parameter: "form_name[password_field_name]"
            csrf_parameter: "form_name[_token]"
            csrf_provider: form.csrf_provider
            intention: authentication

Additionally change setDefaultOptions of your custom form to match this(intention is same as above):

$resolver->setDefaults(array(
        'intention' => 'authentication'
    ));

Finally your form's name:

public function getName()
{
    return 'form_name';
}

The other thing is that you can just as easily configure the login parameters with the form name e.g.

username_parameter: "account_login[username]"
password_parameter: "account_login[password]"
csrf_parameter: "account_login[_token]"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!