从PHP Symfony2动态内容引导模态对话框
Dynamically content bootstrap modal dialog from PHP Symfony2
我正在与bootbox一起使用symfony 2渲染表单。所以我有一个问题当我想动态地改变内容时我不知道怎么做
这就是我想要的
- 我点击一个按钮,从嵌入在模态对话框中的控制器渲染表单
<button class="btn btn-primary btn-new" data-toggle="modal" data-target="#agregarRonda">
Add My Entity
</button>
<div style="display: none;" class="modal fade" id="agregarRonda" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Add my Entity</h4>
</div>
<div class="modal-body">
{% embed "projete:MyEntity:newMyEntity.html.twig" %}
{% endembed %}
</div>
</div>
</div>
</div>
2。当我渲染一个表单newmyentity。html。twig它有一个按钮,重定向到symfony控制器中的这个方法,比如:
public function createMyEntityAction(Request $request)
{
$user = $this->get('security.context')->getToken()->getUser();
$entity = new MyEntity();
$form = $this->createMyEntityForm($entity);
$form->handleRequest($request);
if ($form->isValid())
{
if( ifNotExist( $entity->getDescription() ) )
{
//Do the right things
}
else{
/*
* Return content to the modal dialog and don't hide modal dialog?
*/
}
}
}
我调用ifNotExist方法来检查一些东西。如果返回false,我希望发送内容到模态对话框,而不隐藏模态对话框和修改内容。我该怎么做?
谢谢。
你可以这样做:
public function createMyEntityAction(Request $request)
{
$user = $this->get('security.context')->getToken()->getUser();
$entity = new MyEntity();
$form = $this->createMyEntityForm($entity);
if ($request->getMethod()=="POST"){
$form->handleRequest($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return new Response($entity->getId(),201);
}
}
return $this->render('yourFormTemplate.html.twig', array('form' => $form->createView() );
}
你的实体:
use Symfony'Component'Validator'Constraints as Assert;
...
/**
* MyEntity
* @ORM'Entity()
* @ORM'Table()
*/
class MyEntity
{
...
/**
*
* @Assert'NotBlank(message ="Plz enter the description")
*/
private $description;
...
}
你的JS:
$('#yourAddBtnId').on('click', function(){
var $modal = $('#yourModalId')
$.get("yourURL", null, function(data) {
$modal.find('modal-body').html(data);
})
// create the modal and bind the button
$('#yourModalId').dialog({
buttons: {
success: {
label: "Save",
className: "btn-success",
callback: function() {
that = this;
var data = {};
// get the data from your form
$(that).find('input, textarea').each(function(){
data[$(this).attr('name')] = $(this).val();
})
// Post the data
$.post( "yourURL", function(data , status) {
if ( "201" === status){
$modal.modal('hide');
}
else {
$modal.find('modal-body').html(data);
}
});
}
}
});
});
相关文章:
- 使用模态对话框(JQuery)编辑函数
- jquery模态对话框通过id获取输入类型text的值
- ASP.net按钮单击时不使用Jquery模态对话框激发
- jBox模态对话框Created监听器函数从第二次开始就不起作用了
- 模态对话框与指令在角度,范围问题
- 模态对话框加载 jquery 在控制台中执行,但不从应用程序加载.js
- 我怎样才能用javascript找出模态对话框的宽度和高度
- 在模态对话框中按键事件
- 带有 D3 图的引导模态对话框
- 跨浏览器显示模态对话框替换
- 基于Ionic和AngularJS的模态对话框导致应用程序冻结
- AngularJS 模态对话框表单对象在控制器中未定义
- 引导 3 - 模态背景不会根据模态对话框的高度调整大小
- jqGrid SetCell 和 SaveCell 在关闭模态对话框后将单元格清空
- jqueryUI 模态对话框会破坏 Knockoutjs 绑定
- Webdriver 为模态对话框(这是覆盖)抛出 Nosuchemelent 异常
- 如何在加载网站主页时加载模态对话框
- 如何使用jQuery在模态对话框中访问id
- 如何在jsf-dataTable中调用j-query模态对话框
- 为什么我'在关闭第一模态对话框之后,m不能显示第二模态对话框