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

Paul Maclean picture Paul Maclean · Dec 12, 2012 · Viewed 13.4k times · Source

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?

Answer

jpbaxxter picture jpbaxxter · Aug 11, 2014

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?")'}}) }}