Symfony2 在重新提交表单时转发
Symfony2 forwards when re-submit the form
当我第二次在服务器上发送带有数据的表单时,它会将我重定向到处理来自客户端的请求的路由。
我的意思
我有控制器方法接受来自客户端的请求 AJAX 并验证数据
public function addCommentAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$post->setCreated(new \DateTime('now'));
if ($request->getMethod() == 'POST') {
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
$this->get('session')->getFlashBag()->add('success', 'Comment has been sent on moderation. Wait 1 minute before send another comment.');
}
}
return $this->render('GuestbookBundle:Post:postForm.html.twig', array(
'form' => $form->createView(),
));
}
我的ajax。序列化表单,然后使用表单
中的数据向服务器发送请求
$('.container .form-comment .send-comment').on('click', function(e) {
var $from = $(this).closest('form');
e.preventDefault();
$.ajax({
type: "POST",
url: $from.attr('action'),
data: $from.serialize(),
success: function(data) {
$('.form-comment').empty().append(data);
}
})
});
当我第二次点击发送按钮时,它会将我重定向到页面 mydomain.com/add,该页面处理来自表单的请求
add:
path: /add
defaults: { _controller: GuestbookBundle:Post:addComment }
requirements:
_method: POST
如何解决这个问题?为什么发送成功后我的表单不清晰?
谢谢。
首先,您在提交后 return 再次填写表格,为什么需要这个?
如我所见,您正在尝试添加评论,因此在提交后您可以 return 消息(例如 - 成功)或您需要的任何其他数据。
public function addCommentAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$result = ['status' => 'success',
'comment' => 'Comment has been sent on moderation. Wait 1 minute before send another comment.' ];
$post->setCreated(new \DateTime('now'));
if ($request->getMethod() === 'POST') {
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
// $this->get('session')->getFlashBag()->add('success', 'Comment has been sent on moderation. Wait 1 minute before send another comment.');
return new JsonResponse($result);
}
$result['status'] = 'error';
return new JsonResponse($result);
}
return $this->render('GuestbookBundle:Post:postForm.html.twig', array(
'form' => $form->createView(),
));
}
javascript:
$('.container .form-comment .send-comment').on('click', function(e) {
var $from = $(this).closest('form');
e.preventDefault();
$.ajax({
type: "POST",
url: $from.attr('action'),
data: $from.serialize()
}).done(function(data) {
//data will contains data.status && data.message
//you can add any data in your controller
$('.form-comment').empty().append(data.messages);
}).fail(function(data) {
alert(data.messages);
});
});
您还使用了已弃用的语法,
更好地使用 $form->handleRequest($request);
,你可以阅读它 here
我发现为什么他们在重新提交后在 url 转发我。第二次 jquery 不起作用,因为在响应后我收到新的 html 并且 jquery 可以在按钮上绑定事件。作为解决方案更改 JS 代码
$('.container .form-comment').on('submit', '.send-comment', function(e) {
});
当我第二次在服务器上发送带有数据的表单时,它会将我重定向到处理来自客户端的请求的路由。
我的意思
我有控制器方法接受来自客户端的请求 AJAX 并验证数据
public function addCommentAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$post->setCreated(new \DateTime('now'));
if ($request->getMethod() == 'POST') {
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
$this->get('session')->getFlashBag()->add('success', 'Comment has been sent on moderation. Wait 1 minute before send another comment.');
}
}
return $this->render('GuestbookBundle:Post:postForm.html.twig', array(
'form' => $form->createView(),
));
}
我的ajax。序列化表单,然后使用表单
中的数据向服务器发送请求$('.container .form-comment .send-comment').on('click', function(e) {
var $from = $(this).closest('form');
e.preventDefault();
$.ajax({
type: "POST",
url: $from.attr('action'),
data: $from.serialize(),
success: function(data) {
$('.form-comment').empty().append(data);
}
})
});
当我第二次点击发送按钮时,它会将我重定向到页面 mydomain.com/add,该页面处理来自表单的请求
add:
path: /add
defaults: { _controller: GuestbookBundle:Post:addComment }
requirements:
_method: POST
如何解决这个问题?为什么发送成功后我的表单不清晰?
谢谢。
首先,您在提交后 return 再次填写表格,为什么需要这个? 如我所见,您正在尝试添加评论,因此在提交后您可以 return 消息(例如 - 成功)或您需要的任何其他数据。
public function addCommentAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$result = ['status' => 'success',
'comment' => 'Comment has been sent on moderation. Wait 1 minute before send another comment.' ];
$post->setCreated(new \DateTime('now'));
if ($request->getMethod() === 'POST') {
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
// $this->get('session')->getFlashBag()->add('success', 'Comment has been sent on moderation. Wait 1 minute before send another comment.');
return new JsonResponse($result);
}
$result['status'] = 'error';
return new JsonResponse($result);
}
return $this->render('GuestbookBundle:Post:postForm.html.twig', array(
'form' => $form->createView(),
));
}
javascript:
$('.container .form-comment .send-comment').on('click', function(e) {
var $from = $(this).closest('form');
e.preventDefault();
$.ajax({
type: "POST",
url: $from.attr('action'),
data: $from.serialize()
}).done(function(data) {
//data will contains data.status && data.message
//you can add any data in your controller
$('.form-comment').empty().append(data.messages);
}).fail(function(data) {
alert(data.messages);
});
});
您还使用了已弃用的语法,
更好地使用 $form->handleRequest($request);
,你可以阅读它 here
我发现为什么他们在重新提交后在 url 转发我。第二次 jquery 不起作用,因为在响应后我收到新的 html 并且 jquery 可以在按钮上绑定事件。作为解决方案更改 JS 代码
$('.container .form-comment').on('submit', '.send-comment', function(e) {
});