Symfony2获取所有选中的复选框

时间:2015-04-21 15:08:43

标签: php html symfony checkbox twig

我正在Symfony2项目中工作,我有一个名为File的实体。在一个页面中我有一个文件列表,我为每个文件添加了check-box,我想要的是在文件列表的末尾有一个submit button,当我点击我想要的按钮要删除的文件。我已经实现了将删除文件的action,但我不知道如何获取所选文件,因此我可以将它们传递给deleteAction($files)

这是twig文件的代码:

<tr>
<th>File name</th>                      
<th>Commentary</th>
<th>Size</th>
<th>Product/ SN</th>
<th>Added at(UTC)</th>
</tr>

{% for file in allFiles %}
   <tr>                         
     <td class="name">
       <form name="form1" method="post" action="">
         <input type="checkbox" name="check_file"> {{ file.filename }}
       </form>                              
     </td>                                     

   <td>{{ file.uploaderComment }}</td>
   <td>{{ _self.bytesToSize(file.filesize) }}</td>


   <td>{{ file.product }}</td>
   <td>{{ file.added |date("Y/m/d H:i:s","UTC") }}</td>

  </tr>
 {% endfor %}

2 个答案:

答案 0 :(得分:0)

这就是你要找的东西:

http://symfony.com/doc/current/reference/forms/types/choice.html

Symfony选择表单类型将满足您的需求。

答案 1 :(得分:0)

您可以在控制器中添加处理表单提交的逻辑。

$form->handleRequest($request);

if ($form->isValid()) {
    // perform some action, such as deleting any file with field marked 'delete'
    $data = $form->getData();
    $data->getFiles();
    $deletethese = new ArrayCollection();
    foreach($files as $file){
        if($file->delete() == true){
            $deletethese->add($file);
        }
}

deleteAction($deletethese);

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

请参阅有关form submissions的文档。

相关问题