Get value of a field not declared in FormType

感情迁移 提交于 2019-12-22 01:41:45

问题


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?


回答1:


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



回答2:


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




回答3:


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.




回答4:


After calling $form->bindRequest($request) you can call: $form->getData() to get input from user.

But if you want to receive input data for field that is not mapped you need to use mentioned $request->request->get('field_name').



来源:https://stackoverflow.com/questions/18745880/get-value-of-a-field-not-declared-in-formtype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!