Mil*_*loš 5 dialog symfony twig
我的twig模板中有一个删除链接,我想知道是否有Symfony2方式显示确认对话框.
我知道这可以用JQuery,但也许symfony有自己的"做事方式".
谢谢.
Ziu*_*min 14
只需confirm在删除链接上使用javascript功能即可
<a href="{{ path('delete_route', {csrf:...}) }}" onclick="return confirm('are u sure?')">delete</a>
Run Code Online (Sandbox Code Playgroud)
我知道这个主题有点旧但我用另一种方法在删除对象之前显示确认消息.
我认为与寻找另一种解决方案而不是javascript的人分享很有意思.
它比上述解决方案更复杂,或者至少更长.
首先,我将这些操作添加到我的控制器中
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'));
}
Run Code Online (Sandbox Code Playgroud)
所以在我的模板中有我的对象列表,我将删除按钮指向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 %}
Run Code Online (Sandbox Code Playgroud)
我把这些twig代码放在我的上层模板中,以便为我想要的任何对象重用该消息.如果我想删除另一种类型的对象,我只使用传递给会话变量的信息来自定义消息和pathes.如果您转到直接网址,则不会删除您的对象.
我不知道这是否是最好的方法.您的建议将不胜感激.
谢谢.
| 归档时间: |
|
| 查看次数: |
16339 次 |
| 最近记录: |