Symfony Form Class: rename fields name attribute&#

2020-08-02 02:36发布

问题:

Is there a way to set individual form fields name in the FormBuilder?

What I mean is normally you get is form_name[field_name], or if the form_name is an empty string you get field_name.

Now I want to ask if it is possible to rename individual field name and the form still works. Something like this:

First field name="form_name[field_name]"
Second field name="renamed_fildname"
Third field name="form_name[field_name]"
etc...

So is this possible?

More explanation: this is what I am trying to achieve: Default Symfony User Entity with Form Class
I Figured out that the default login requires the input field named: _username and _password, but the form doesn't build that. But the form to be valid its needs the _token field.

Now if I try to add a name to the form:

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

it will but all the names into this form: _[field_names] which is good for the token but not good for my username and password field. its need to be _username and _password but it actually is _[username] and _[password] which isn't good for me, nor for the Security Bundle which simply throws me Bad Credentials.

回答1:

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 ;)



回答2:

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';
}


回答3:

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]"