How to remove an object from an entity list template (CRUD)?

Explanation:

I created a CRUD object, getting the following default actions:

  • indexAction (): lists all entities.
  • showAction ($ id): Finds (by identifier) ​​and displays the object.
  • deleteAction ($ id): deletes the object.
  • other actions.

So, I saw that I can only delete the entity in actions that use the $ id parameter (for example: showAction ($ id)), but I want to delete the object inside the indexAction template, because I keep the step to the users.

Removal requires a request, identifier, and POST method.

I tried to code something like:

<a href="{{ path('entity_delete', { 'id': entity.id }) }}" class="btn"> <img src="{{ asset('bundles/acme/images/delete.png') }}" ... /> </a> 

When I perform an action, I get the following error:

No route found for "GET / acme / something / 4 / delete": method not allowed (Allow: POST, DELETE)

This answer is clear and this is what I expected, so I tried to do something similar, but using the form. Something like that:

 <form id="formDelete" action="{{ path('entity_delete', { 'id': entity.id }) }}" method="post"> <input type="hidden" name="_method" value="DELETE" /> {{ form_widget(delete_form) }} <a href="{{ url('entity_delete') }}" class="btn" onclick="document.getElementById('formDelete').submit();"> <img src="{{ asset('bundles/acme/images/delete.png') }}" ... /> </a> </form> 

But the line {{ form_widget(delete_form) }} is a problem because indexAction() has no parameter and needs this code:

 $deleteForm = $this->createDeleteForm($id); return $this->render('AcmeBundle:Demo:index.html.twig', array( 'entities' => $entities, 'delete_form' => $deleteForm->createView(), )); 

As you can see, the $ id parameter is required for the createDeleteForm($id) method, but I cannot get it from indexAction() .

Question:

What is the best way to solve this problem?

+7
forms crud symfony
source share
1 answer

if you want to have as many delete buttons as there are elements in your index, how easy it is.

In the indexAction, add the following loop and remember to pass this parameter to the view.

 public function indexAction() { $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository('FooBundle:Link')->findAll(); $deleteForms = array(); foreach ($entities as $entity) { $deleteForms[$entity->getId()] = $this->createDeleteForm($entity->getId())->createView(); } return array( 'entities' => $entities, 'deleteForms' => $deleteForms, ); } 

Basically, I just iterate over all my entities and create the corresponding delete form using the built-in method generated by crud, storing each form in an array and passing it to the view.

Then, in the view, simply add the form already in the generated edit.html.twig form, and edit the form_widget parameter:

 <form action="{{ path('foo_delete', { 'id': entity.id }) }}" method="post"> <input type="hidden" name="_method" value="DELETE" /> {{ form_widget(deleteForms[entity.id]) }} <button type="submit" class="btn btn-small"> <i class="icon-trash"></i> {{ 'links.admin.form.delete' | trans({}, 'FooBundle') }} </button> </form> 
+24
source share

All Articles