Skip to content

Instantly share code, notes, and snippets.

@cakper
Created July 29, 2012 20:09
Show Gist options
  • Save cakper/3201592 to your computer and use it in GitHub Desktop.
Save cakper/3201592 to your computer and use it in GitHub Desktop.
Delete form
<?php
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
/**
* Deletes a Report entity.
*
* @Route("/{id}/delete", name="report_delete")
* @Secure(roles="ROLE_ADVISOR")
* @Method("post")
*/
public function deleteAction($id)
{
$form = $this->createDeleteForm($id);
$request = $this->getRequest();
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('CakperReportBundle:Report')->find($id);
if (!$entity || ($entity->getUser() != $this->get('security.context')->getToken()->getUser())) {
throw $this->createNotFoundException('Nie można odnaleźć raportu bądź nie posiadasz uprawnień do jego usuwania.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('report'));
}
/**
* Displays a form to edit an existing Report entity.
*
* @Route("/{id}/edit", name="report_edit")
* @Secure(roles="ROLE_ADVISOR")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('CakperReportBundle:Report')->find($id);
$editForm = $this->createForm(new ReportType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
<form action="{{ path('report_delete', { 'id': entity.id }) }}" method="post">
<div class="">
{{ form_widget(delete_form) }}
<button type="submit" class="btn btn-danger">Usuń raport (tej akcji nie można anulować)</button>
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment