How to get form values in Symfony2 controller

Simply :

$data = $form->getData();

In Symfony >= 2.3, you can get the value of single fields with:

$var = $form->get('yourformfieldname')->getData();

On the other hand, you can use:

$data = $form->getData();

BUT this would get you two different things:

  • the entity with values populated by the form, if your form have the data-class option enabled (so it's binded to an entity); this will exclude any field with the 'mapping' => false option

  • otherwise, an array with all the form's fields


None of the above worked for me. This works for me:

$username = $form["username"]->getData();
$password = $form["password"]->getData();

I hope it helps.


In Symfony 2 ( to be more specific, the 2.3 version ) you can get a data of an field by

$var = $form->get('yourformfieldname')->getData();

or you can get all data sent

$data = $form->getData();

where '$data' is an array containing your form fields' values.

Tags:

Forms

Symfony