簡體   English   中英

Symfony:獲取現有表單參考

[英]Symfony: get existing form reference

在將網站遷移到symfony時,我必須重用一些HTML代碼。 當前的HTML頁面有一個表單:

<form name="n1" id="n1" method="post" onsubmit="return verifSelect()" action="{{path('toto')}}" >

     <select>
         {% for e in experimentations %}
         <option>{{e.name}}</option>
         {% endfor %}
     </select>

</form>

我想將所選的Experimentation對象(不僅是名稱)傳遞給toto頁面。 symfony doc使用如下內容

if ($form->isSubmitted() && $form->isValid()) {
        // ... perform some action, such as saving the task to the database

        return $this->redirectToRoute('task_success');
}

但我有兩個問題:

  • 如何獲取form對象引用?
  • 如何獲取所選的experimentation對象參考(因此我可以將其作為參數發送到下一個路徑)?

非常感謝你的幫助,

我不確定我理解你的問題,但我會盡力給你答案:

  • 您可以在控制器中使用$ form-> getData()來獲取表單數據。
  • 您無法將完整對象作為參數傳遞給下一個路徑。

你可以這樣做:

$data = $form->getData();
return $this->redirectToRoute('task_success',array('experimentation'=> $data['experimentation');

那么你可以在你的totoAction上獲得實驗ID。

我希望它對你有所幫助。

首先,您必須為您的選擇標記命名,以便您可以引用它name="experimentations" 你還需要一個提交按鈕,我猜你有<input type="submit" value="submit"/>

<form name="n1" id="n1" method="post" onsubmit="return verifSelect()" action="{{path('toto')}}" >

     <select name="experimentations">
         {% for e in experimentations %}
         <option>{{e.name}}</option>
         {% endfor %}
     </select>
     <input type="submit" value="submit"/>

</form>

要獲取控制器類中的信息,請使用$request -Object。 功能

class DefaultController extends Controller {
    /**
     * 
     * Matches /todo/
     * 
     * @Route("/todo/", name="Todo")
     */
    public function indexAction(Request $request) {
         $experimentations = $request->request->get("experimentations");
         ...
    }

據我所知,在這種情況下你不能使用form-Object,但人們可能會糾正我......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM