javascript 如何在不重新加载页面的情况下在服务器响应后在 Symfony2 中显示警报或信息消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25628545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display alert or info messages in Symfony2 after a server response without reloading the page
提问by AymanKun
I am building an admin application for opticians with Symfony2. When the admin adds a new customer to the database, my controller checks if the customer name is duplicate or not. I want to display a popup dialog that asks the user if he wants to add the new customer anyway or not. How can I implement this? Should I use Ajax? Here is a sample code from the controller I am using in this case :
我正在使用 Symfony2 为配镜师构建一个管理应用程序。当管理员向数据库添加新客户时,我的控制器会检查客户名称是否重复。我想显示一个弹出对话框,询问用户是否要添加新客户。我该如何实施?我应该使用 Ajax 吗?这是我在这种情况下使用的控制器的示例代码:
public function nouveauAction(Request $request)
{
$form = $this->createFormBuilder()
->add('nom','text')
->add('tel','text', array('label' => 'No de téléphone', 'data' => '06'))
->add('email','email', array('label' => 'E-mail', 'required' => false))
->add('date','date', array('label' => 'Date d\'ajout', 'data' => new \DateTime()))
->add('ajouter','submit')
->getForm()
;
$form->handleRequest($request);
if ($form->isValid()){
$client = new Client();
$client->setNomClient($form["nom"]->getData());
$client->setTelClient($form["tel"]->getData());
$client->setEmailClient($form["email"]->getData());
$client->setDateEditionClient($form["date"]->getData());
//just for now (Later we'll retrieve the username from the session)
$em = $this->getDoctrine()->getEntityManager();
$user = (new Utilisateur)->rechercherParPseudo($em, 'admin');
$client->setIdUtilisateur($user);
$em = $this->getDoctrine()->getEntityManager();
if($client->existe($em))
{
//I need a popup message here : The customer you are trying to add already exists""
}
else
{
$request = $this->container->get('request');
if($client->existeNomDouble($em)) //If the customer name is duplicate
{
//I need a popup message here with Yes/No buttons...
}
else
{
//Writing to the database:
$em = $this->getDoctrine()->getEntityManager();
$client->ajouterClient($em);
//A notification to fade in here : "Customer successfully added"
}
}
}
return $this->render('ClientBundle:Client:nouveau.html.twig', array(
'formAjouter' => $form->createView(),
));
}
回答by Jihed Ben taher
Try this :
试试这个 :
Controller Side :
控制器端:
$this->get('session')->getFlashBag()->add(
'notice',
'Customer Added!'
);
View Side (Twig) :
视图侧(树枝):
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="alert alert-success">
{{ flashMessage }}
</div>
{% endfor %}
回答by John B
Are you asking if you should use ajax for displaying the popup after a button has been clicked? If so, I would recommend using ajax. Here is a Bootstrap library I have used before in symfony that should come in handy for this: http://vitalets.github.io/x-editable/
您是在问是否应该在单击按钮后使用 ajax 来显示弹出窗口?如果是这样,我会建议使用ajax。这是我之前在 symfony 中使用过的 Bootstrap 库,它应该会派上用场:http: //vitalets.github.io/x-editable/