I'm working in a Symfony2
project, and i have an entity called File
. In a page i have a list of files and i added a check-box
for each file, and what i want is to have a submit button
at the end of the files list and when i click the button i want the files to be deleted. I've already implement the action
that will delete the files but i don't know how to get the selected files so i can pass them to the deleteAction($files)
.
here is the code of the twig file:
<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 %}
This is what you are looking for:
http://symfony.com/doc/current/reference/forms/types/choice.html
The Symfony choice form type will take care of your needs.
You can add the logic in the controller that handles the form submission.
$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');
}
Refer to the documentation about form submissions.