Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony and Twig: How to display a confirmation dialog

I have a delete link in my twig template and I would like to know if there is a Symfony2 way of displaying the confirm dialog.

I know this is possible with JQuery, but maybe symfony has his own "way of doing".

Thank you.

like image 694
Milos Cuculovic Avatar asked Dec 04 '12 08:12

Milos Cuculovic


2 Answers

Just use confirm javascript function on your delete link

<a href="{{ path('delete_route', {csrf:...}) }}" onclick="return confirm('are u sure?')">delete</a>
like image 93
Ziumin Avatar answered Sep 22 '22 15:09

Ziumin


I know this topic is a bit old but i used another way to display confirmation message before to delete an object.

I think is interesting to share with people looking for another solution than javascript.

It's a bit more complicated, or at least longer than the above solution.

First I add these actions to my controler

public function confirmAction(Capteur $myobject) {
    // check my object exist
    if (!$myobject) {
        // throw error
    } else {
        // you can pass information about your object to the confirmation message
        $myobjectInfo = array(
            'yes' => 'path_if_confirm', // path to the deleteAction, required
            'no' => 'path_if_dont_confirm', // path if cancel delete, required
            // put any information here. I used type, name and id
            // but you can add what you want
            'type' => 'mytype', 
            'id' => $myobject->getId(), // required for deleteAction path
            'name' => $myobject->getName()
        );
        // add informations to session variable
        $this->get('session')->set('confirmation',$myobjectInfo);
        return $this->redirect($this->generateUrl('confirmation_template_path'));
    }
}
public function deleteAction(MyType $myobject) {
    if (!$myobject) {
        // throw exception
    } else {
        $request =  $this->get('request');
        if ($request->getMethod() == 'POST') {
            $em = $this->getDoctrine()->getManager();
            $em->remove($myobject);
            $em->flush();
            $this->get('session')->getFlashBag()->add('success', 'Nice shot.');
        } else {
              // you can do something here if someone type the direct delete url.
        }
    } 
    return $this->redirect($this->generateUrl('where_you_want_to_go'));     
}

So in my template with my list of object, I point the delete button to confirmAction.

Then in the confirmation_template (or in my case in the upper parent template layout.hml.twig) I add this

 {% if app.session.get('confirmation') is defined and app.session.get('confirmation') is not null %}
    <div>
        <p>
        put your message here. You can use information passed to the session variable {{ app.session.get('confirmation').type }} {{ app.session.get('confirmation').name }} {{ app.session.get('confirmation').id }} etc..
        </p>
        <form method="post" action="{{ path(app.session.get('confirmation').yes,{'id':app.session.get('confirmation').id }) }}">
            <button type="submit" class="btn red">confirm and delete</button>
        <a href="{{ path(app.session.get('confirmation').no) }}" class="btn blue">Cancel</a>
        </form>
    </div>
    # put confirmation variable to null, to disable the message after this page #
    {{ app.session.set('confirmation',null) }}
{% endif  %}

I put these twig code in my upper template in order to reuse the message for any object i want. If i want to delete another type of object, i just use information passed into session variable to custom the message and the pathes. If you go to the direct url, your object won't be deleted.

I don't know if it's the best way to do that. Your advices will be appreciated.

Thanks.

like image 44
olibiaz Avatar answered Sep 22 '22 15:09

olibiaz