What is the best way to add a "confirm-option" to a delete form in Symfony2?
Asked Answered
J

4

5

If you create CRUD-code for an entity in Symfony2 using the console, you will end up with a very basic delete function.

This function is lean and efficient, but does not provide an "are you sure?"-confirmation. If the entity to be deleted exists, it will be deleted immediately.

Does anyone have suggestions for the easiest way to add user confirmation?

Up until now I have been using:

  • An extra controller function
  • jQuery

It seems a bit weird though that Symfony2 would have no built-in option for this. Anyone better ideas?

Jorry answered 12/12, 2012 at 8:23 Comment(4)
why jQuery or simply Javascript isn't suitable for you?Unhandsome
Well, it is but I would have imagined that Symfony2 would have a built-in option for it. I guess it hasn't and javascript is the way to go.Jorry
I'd suggest a javascript confirmation, and if the data is important, possibly use a softdelete extension so that entities aren't deleted from the database entirely.Giveandtake
OK thanks Mike, I will build it like you suggest. So consider your answer accepted ;)Jorry
L
10

You could also render the basic delete-field of your form with an additional attribute:

Inside your Twig-Template:

{{ form(delete_form, {'attr': {'onclick': 'return confirm("Are you sure?")'}}) }}
Lowrance answered 11/8, 2014 at 12:44 Comment(1)
I used this approach but the render is duplication onclick inside a div within the form, making click display message twice.Thornton
A
7

Just use confirm javascript function on your delete link

<a href="{{ path('delete_route', {csrf:...}) }}" onclick="return confirm('are u sure?')">delete</a>
Alarise answered 14/4, 2013 at 2:52 Comment(0)
M
5

An issue with:

{{ form(delete_form, {'attr': {'onclick': 'return confirm("Are you sure?")'}}) }}

Is that it causes a double confirmation box to pop up. A better solution is to place it inside the controller on the deleteForm...

private function createDeleteForm($id)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('my_controller_delete', array('id' => $id)))
        ->setMethod('DELETE')
        ->add('submit', 'submit', array('label' => 'Delete',
                'attr' => array(
                        'onclick' => 'return confirm("Are you sure?")'
                )))
        ->getForm();
} 

Then the onclick function is on the button instead of the form.

Marriage answered 16/10, 2015 at 17:45 Comment(0)
B
0

A lightweight solution is to actually show your form twice: First, the user fills all fields. Then we recognise that it has not been confirmed already so we re-render it. This time the javascript immediately presents the confirmation dialog.

To this end we add a hidden field confirmed to the form and a parameter to the twig rendering:

$builder = $this->createFormBuilder(...);
$builder->add('confirmed', HiddenType::class, ['mapped' => false]);

$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

    if($form->get('confirmed')->getData() != true) { 
        // re-render with confirmation flag set
        return $this->render('default/form.html.twig', [
             'form' => $form->createView(),
             'confirm' => true]);
    }
} else {
    return $this->render('default/form.html.twig', [
         'form' => $form->createView(),
         'confirm' => false]);
}

And in your template to add a javascript like:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

<script>
$(document).ready(function () {
    var c = confirm('Really sure?');
    if(c) {
        $('#form_confirmed').val(true);
        $('form')[0].submit();

    }
});
</script>
Bless answered 30/4, 2018 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.