Get value of a field not declared in FormType

user2740782 picture user2740782 · Sep 11, 2013 · Viewed 19.1k times · Source

I have a form declared in nameType.php and the view render all field but I want add another field manually.

Form:

<form action="{{ path('create') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}
    <input type="text" value="2">
   </form>

And get the values in the controller:

$form->bindRequest($request);

How can I collect the value of the input in the controller?

Answer

albert picture albert · Sep 11, 2013

If you are trying this because the form is linked to your entity field you can add a field to FormType as not mapped. Then you do not need getters and setters on your entity.

->add("inputName", "text", array("mapped"=>false, "data"=>2, "label"=>false))

To get the data in the controller:

$form->get("inputName")->getData();