Reputation: 57
I have a list with users from a db which has a working delete button. Here's my controller:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Users;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\HttpFoundation\Session\Session;
class DefaultController extends Controller
{
/**
* @Route("/homepage", name="homepage")
*/
public function listAction(Request $request)
{
$users = new Users();
$userssrc = null;
$name = null;
$session = $request->getSession();
$defaultData = array('message' => 'wut');
$formsearch = $this->createFormBuilder($defaultData)
->add('search', TextType::class, array('label'=>'Suche', 'attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
->add('submit', SubmitType::class, array('label'=>'suchen','attr'=>array('class'=>'btn btn-primary')))
->add('reset', SubmitType::class, array('label'=>'zurücksetzten', 'attr'=> array('class'=>'btn btn-default')))
->getForm();
$formsearch->handleRequest($request);
if($formsearch->isSubmitted() && $formsearch->isValid()){
$name = $formsearch['search']->getData();
$em=$this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT u
FROM AppBundle:Users u
WHERE u.vorname = :name
OR u.nachname = :name')->setParameter('name', $name);
$userssrc = $query->getResult();
if ($formsearch->get('reset')->isClicked()){
return $this->redirectToRoute('homepage');
}
}
else{
$vae = 'no form submission';
dump($vae);
}
//gets the table entries
$list = $this->getDoctrine()
->getRepository('AppBundle:Users')
->findAll();
// Returns to index.html.twig with the variable liste, in which the db entries are
return $this->render('main/index.html.twig', array('list'=>$list, 'form'=>$formsearch->createView(), 'userssrc'=>$userssrc));
//return $this->render('base.html.twig', array('session'=>$session));
}}
and here's the index.html.twig file:
{% extends 'base.html.twig' %}
{% block title %}Personeneinträge{% endblock %}
{% block body %}
<div class='container'>
<h1>Personeneinträge</h1>
<form class='form-inline' method="post">
{{ form_start(form)}}
{{ form_widget(form)}}
{{ form_end(form)}}
</form>
<table class="table table-hover">
<thead>
<th>Vorname</th>
<th>Nachname</th>
<th>Strasse</th>
<th>Ort</th>
<th>PLZ</th>
<th>Beschreibung</th>
<th class='col-md-3'>Bild</th>
<th> </th>
</thead>
<tbody>
{% if userssrc is null %}
{% for row in list %}
<tr>
<td>{{row.vorname}}</td>
<td>{{row.nachname}}</td>
<td>{{row.strasse}}</td>
<td>{{row.ort}}</td>
<td>{{row.plz}}</td>
<td>{{row.beschreibung}}</td>
{% if row.bild %}
<td><img class='usrimg' src="{{asset(row.bild, 'uploaded_files')}}" alt="Bild zum Benutzer"></td>
{% else %}
<td>-</td>
{% endif %}
<td>
<a href="{{ path('details',{'id':row.id})}}" class="btn btn-success">Details</a>
<a href="{{ path('edit',{'id':row.id})}}" class="btn btn-info">Bearbeiten</a>
<a href="{{ path('delete',{'id':row.id})}}" class="btn btn-danger">Löschen</a>
<a href="{{ path('mail',{'id':row.id})}}" class="btn btn-default">Als Mail senden</a>
</td>
</tr>
{% endfor %}
{% else %}
{% for row in userssrc %}
<tr>
<td>{{row.vorname}}</td>
<td>{{row.nachname}}</td>
<td>{{row.strasse}}</td>
<td>{{row.ort}}</td>
<td>{{row.plz}}</td>
<td>{{row.beschreibung}}</td>
<td>
<a href="{{ path('details',{'id':row.id})}}" class="btn btn-success">Details</a>
<a href="{{ path('edit',{'id':row.id})}}" class="btn btn-info">Bearbeiten</a>
<a href="{{ path('delete',{'id':row.id})}}" class="btn btn-danger">Löschen</a>
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</div>
{% endblock %}
I now want it to ask for a confirmation before it deletes the entry, does anyone know a simple solution?
Just in case it helps to understand, here's a screenshot of the rendered view:
I'm sorry, forgot to add my deleteAction:
/**
* @Route("/loeschen/{id}", name="delete")
*/
public function deleteAction($id){
//start Doctrine
$em=$this->getDoctrine()->getManager();
$list = $em->getRepository('AppBundle:Users')->find($id);
return $this->redirectToRoute('homepage', array('remove'));
$em->remove($list);
$em->flush();
}
Upvotes: 1
Views: 1584
Reputation: 893
Whatever you like. Some common methods;
Create an intermediary page with just a button when deleting as confirmation (Are you sure?). Some of symfony's generators use this 'DeleteForm' type of thing. Where it links to a new page with the ID using the route, and on POST it actually deletes.
You can add an unmapped checkbox which would not attempt to save it on the object. Just check if it is checked in the controller. (although that doesn't really fit your list layout)
And there is javascript;
And im sure there are other options too.
Upvotes: 2