2

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.

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah i know i make a few things complicated, but that's the only way i know how to learn new stuff and what the limits are. and thanks for the clear answer that what i am trying to do is impossible.
I agree this is the correct answer for this use case, but there are cases that you need to be able to do this in symfony. in that case, you can just use {{ form_row(form.name, {'full_name':'name'}) }} but don't expect to bind the request to the form afterwards. I use this in some cases when working with angularJs to manage the data model.
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';
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.