Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of a field not declared in FormType

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?

like image 271
user2740782 Avatar asked Sep 11 '13 15:09

user2740782


3 Answers

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();
like image 154
albert Avatar answered Nov 16 '22 15:11

albert


You can not retrieve the input value from the $form, because it's not part of it.

You have to retrieve it from the request in the Controller by using the name attribute :

HTML : <input type="text" value="2" name"var_name">

Controller: $request->request->get('var_name')

like image 29
S.Thiongane Avatar answered Nov 16 '22 15:11

S.Thiongane


how could collect the value of the input to the controller?

The instant-gratification way would be to use

$form->get('inputName')->getViewData()

for an unmapped field. But I'm sure there are better ways which are Symfony validation-compliant.

like image 3
Bango Avatar answered Nov 16 '22 16:11

Bango