2014-03-31 71 views
2

使用Symfony2.3.​​4。symfony2創建沒有實體類的表單

我想創建一個窗體而不使用一個類型,它實際上是一個非常小的窗體,只有兩個選擇從數據庫加載它們的選項,到目前爲止它的工作原理,我不能做的是獲取窗體數據(在控制器中)提交時。我試圖按照指令here,但我無法做到。

這是到目前爲止我的代碼:

控制器: 功能將數據傳遞到窗體:

public function selectAction($id, $id_actpost){ 
     $em = $this->getDoctrine()->getManager(); 

     $entity = $em->getRepository('ActPostBundle:Edition')->find($id); 
     $students = $em->getRepository('PersonBundle:Students')->findAll(); 
     $students2 = $em->getRepository('PersonBundle:ForeignStudents')->findAll(); 

     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find Edicion entity.'); 
     } 

     return $this->render('ActPostBundle:Edition:select.html.twig', array(
        'entity' => $entity, 
        'id_actpost' => $id_actpost, 
        'students' => $students, 
        'foreignstudents' => $students2, 
     )); 
    } 

HTML片段關於窗體本身:

<form class="form-horizontal sf_admin_form_area" 
       action="{{ path('edition_update_selected', 
      { 'id': entity.id, 'id_actpost': id_actpost }) }}" 
       method="post" enctype="multipart/form-data"> 
      <div style="margin-left: 80px" class="row-fluid"> 
       <div class="span12"> 
        <select name="students" multiple="multiple"> 
        {% for s in students %} 
        <option {%if s in entity.students%}selected="selected"{%endif%}> 
         {{s}}</option> 
        {%endfor%} 
        </select> 
       </div> 
      </div> 

      <div class="row-fluid"> 
       <select name="students2" multiple="multiple"> 
        {% for s in students2 %} 
        <option {%if s in entity.foreignstudents%}selected="selected" 
         {%endif%}>{{s}}</option> 
        {%endfor%} 
       </select> 
      </div> 
      <div class="form-actions"> 
       <button type="submit" class="btn btn-primary"> 
        <i class="glyphicon-refresh"></i> {{'Update' | trans}}</button> 
       <a class="btn" href="{{ path('edition', {'id_actpost' : id_actpost }) }}"> 
        <i class="glyphicon-ban-circle"></i> {{'Cancel' | trans }} 
       </a> 
      </div> 
     </form> 

這裏是我從鏈接發佈的開頭讀取的: 函數獲取提交的數據並更新數據庫,雖然數據庫部分可以保持忽略,直到我設法從表單中獲得數據:

public function update_selectedAction(Request $request, $id, $id_actpost) { 
     $em = $this->getDoctrine()->getManager(); 

     $entity = $em->getRepository('ActPostBundle:Edition')->find($id); 

     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find Edicion entity.'); 
     } 

     $defaultData = array('message' => 'Type here'); 
     $editForm = $this->createFormBuilder($defaultData) 
       ->add('students','choice') 
       ->add('students2', 'choice') 
       ->getForm(); 

     $editForm->handleRequest($request); 

我想知道如果我讀實際上是我所需要的,因爲雖然我覺得這是我可能是錯的,所以任何關於這件事情的見解,甚至任何其他方式來做到這一點將非常感激。

回答

3

你應該用symfony中的表單生成器生成的形式在update_selectedAction()

public function update_selectedAction(Request $request, $id, $id_actpost) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $entity = $em->getRepository('ActPostBundle:Edition')->find($id); 
    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find Edicion entity.'); 
    } 
    $defaultData = array('message' => 'Type here'); 
    $form = $this->createFormBuilder($defaultData) 
     ->add('students', 'entity',array('class' => 'PersonBundle:Students', 
      'property' => 'students','expanded'=>false,'multiple'=>false)) 
     ->add('students2', 'entity',array('class' => 'PersonBundle:ForeignStudents', 
        'property' => 'foreignstudents','expanded'=>false,'multiple'=>false)) 
    ->add('submit','submit') 
    ->getForm(); 
    if ($request->getMethod() == "POST") { 
     $form->submit($request); 
     if ($form->isValid()) { 
      $postData = current($request->request->all()); 
      var_dump($postData); /* All post data is here */ 
      /* echo $postData['students']; */ 
      /* echo $postData['students2']; */ 
      /* 
      * do you update stuff here 
      * */ 
     } 
    } 
    return $this->render('ActPostBundle:Edition:select.html.twig', array('form'=>$form->createView())); 
} 

在您的樹枝即ActPostBundle:Edition:select.html.twig使您的形式從評論

{{ form(form) }} 

編輯你的樹枝文件渲染你的表格,如

{{ form_errors(form) }} 
{{ form_row(form.students) }} 
{{ form_row(form.students2) }} 
{{ form_row (form._token) }} 
<input type="submit"> /* your submit button */ 

從評論編輯2

如果你想將文本中選擇框的值屬性,你可以使用選擇字段類型

$students = $em->getRepository('PersonBundle:Students')->findAll(); 
$students2 = $em->getRepository('PersonBundle:ForeignStudents')->findAll(); 
$studentsArray=array(); 
$students2Array=array(); 
foreach($students as $s){ 
$studentsArray[$s->getStudents()]=$s->getStudents(); 
} 
foreach($students2 as $s){ 
$students2Array[$s->getForeignstudents()]=$s->getForeignstudents(); 
/* here array key part will be the value of selectbox like $students2Array['your val to get in post data'] */ 
} 
$form = $this->createFormBuilder($defaultData) 
    ->add('students', 'choice',array('choices' => $studentsArray,'expanded'=>false,'multiple'=>false)) 
    ->add('students2', 'choice',array('choices' => $students2Array,'expanded'=>false,'multiple'=>false)) 
     ->add('submit','submit') 
     ->getForm(); 
+0

,但如果我這樣做的,我會修改selectAction,對吧?因爲它是,它不會發送**表單**變量到.twig。請記住,selectAction是將當前信息加載到表單中,並使用update_selectedAction來獲取提交的數據,並且它們都必須與.twig進行交互。 – jmiguel

+0

使用這種方法後,你不需要'selectAction',你可以直接使用'update_selectedAction'來構建表單,並且還可以保存表格 –

+0

,我只是做了你告訴我的,現在這個東西是$ request-> getMethod( )==「POST」不是真的,所以我嘗試了** $ editForm-> submit($ request); **沒有** if **之前,然後** var_dump($ editForm-> getErrors() ); die(); ** 檢查錯誤,我得到這個錯誤對象的一些屬性,其中之一是: 'message'=>'CSRF令牌無效。請嘗試重新提交表單。' 響鈴...? – jmiguel

相關問題