【问题标题】:Symfony2 get all selected checkboxSymfony2 获取所有选中的复选框
【发布时间】:2015-06-28 20:20:29
【问题描述】:

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

这是树枝文件的代码:

<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 %}

【问题讨论】:

    标签: php html symfony checkbox twig


    【解决方案1】:

    这就是你要找的:

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

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

    【讨论】:

    • 我不会在我的示例中使用表单生成器,我只想删除选定的文件
    【解决方案2】:

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

    $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 的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 2012-02-14
      • 2016-11-06
      • 2011-04-19
      • 2010-10-10
      • 1970-01-01
      相关资源
      最近更新 更多