2012-12-04 17 views
5

मेरे पास मेरे twig टेम्पलेट में एक डिलीट लिंक है और मैं जानना चाहता हूं कि पुष्टि संवाद प्रदर्शित करने के लिए सिम्फनी 2 तरीका है या नहीं।सिम्फनी और ट्विग: एक पुष्टिकरण संवाद कैसे प्रदर्शित करें

मुझे पता है कि यह JQuery के साथ संभव है, लेकिन शायद सिम्फनी का अपना "तरीका करने का तरीका" है।

धन्यवाद।

+0

मुझे नहीं लगता कि symfony2 में ऐसी चीज है। –

+0

@habeebperwad, आपकी राय के लिए धन्यवाद। –

उत्तर

11

बस अपने हटाना लिंक

<a href="{{ path('delete_route', {csrf:...}) }}" onclick="return confirm('are u sure?')">delete</a> 
+1

धन्यवाद, मैंने जोड़ा कि अगर मैं ठीक या रद्द करता हूं, तो परिणाम वही है, मेरा नियंत्रक हटाए गए क्रिया को संसाधित करेगा। –

+1

क्या आपके पास कोई अतिरिक्त क्लिक हैंडलर हैं जो इस लिंक से जुड़े हैं? अगर रद्द करें बटन क्लिक किया गया है और 'पुष्टि' 'क्या आप निश्चित हैं?') – Ziumin

+0

वास्तव में, हटाएं लिंक छवि बटन में एम्बेड किया गया है, तो आपका ब्राउज़र पृष्ठ को हटाने के लिए नहीं जाना चाहिए। मैं अपने प्रश्न को अपने डिलीट बटन के ट्विग कोड के साथ संपादित करूंगा। –

5

पर confirm जावास्क्रिप्ट समारोह का उपयोग मैं जानता हूँ कि इस विषय थोड़ा पुराना है, लेकिन मैं एक वस्तु को नष्ट करने से पहले पुष्टि संदेश प्रदर्शित करने के लिए एक और तरीका इस्तेमाल किया।

मुझे लगता है कि जावास्क्रिप्ट की तुलना में किसी अन्य समाधान की तलाश करने वाले लोगों के साथ साझा करना दिलचस्प है।

यह थोड़ा अधिक जटिल है, या उपरोक्त समाधान से कम से कम लंबा है।

सबसे पहले मैं अपने 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'));  
} 

तो वस्तु की अपनी सूची के साथ अपने टेम्पलेट में, मैं confirmAction को हटाएँ बटन इशारा करते हैं।

फिर confirmation_template में (या ऊपरी माता पिता टेम्पलेट layout.hml.twig में मेरे मामले में) मैं जोड़ने के इस

{% 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 %} 

मैं आदेश के लिए संदेश पुन: उपयोग करने में मेरी ऊपरी टेम्पलेट में इन टहनी कोड डाल कोई वस्तु मैं चाहता हूँ। अगर मैं किसी अन्य प्रकार की वस्तु को हटाना चाहता हूं, तो मैं संदेश और पैच को अनुकूलित करने के लिए सत्र चर में पारित जानकारी का उपयोग करता हूं। यदि आप सीधे यूआरएल पर जाते हैं, तो आपकी ऑब्जेक्ट को हटाया नहीं जाएगा।

मुझे नहीं पता कि यह करने का सबसे अच्छा तरीका है या नहीं। आपकी सलाह की सराहना की जाएगी।

धन्यवाद।

संबंधित मुद्दे