Php Symfony2在重新提交表单时转发
当我第二次在服务器上发送包含数据的表单时,它会在处理来自客户端的请求的路由上重定向我 我的意思是 我有一个控制器方法,它将来自客户端的请求作为AJAX接受并验证数据Php Symfony2在重新提交表单时转发,php,jquery,ajax,forms,symfony,Php,Jquery,Ajax,Forms,Symfony,当我第二次在服务器上发送包含数据的表单时,它会在处理来自客户端的请求的路由上重定向我 我的意思是 我有一个控制器方法,它将来自客户端的请求作为AJAX接受并验证数据 public function addCommentAction(Request $request) { $post = new Post(); $form = $this->createForm(new PostType(), $post); $post->set
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(),
));
}
我的阿贾克斯。序列化表单,然后使用表单中的数据将请求发送到服务器
$('.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);
}
})
});
add:
path: /add
defaults: { _controller: GuestbookBundle:Post:addComment }
requirements:
_method: POST
当我第二次单击“发送”按钮时,它将我重定向到mydomain.com/add页面,该页面处理来自表单的请求
$('.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);
}
})
});
add:
path: /add
defaults: { _controller: GuestbookBundle:Post:addComment }
requirements:
_method: POST
如何解决这个问题?为什么成功后发我的表格不清楚
谢谢。首先,您在提交表单后再次返回表单,为什么需要这个? 正如我看到的,您正在尝试添加评论,所以在提交后,您可以返回消息(例如-success)或任何其他您需要的数据
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)代码>,你可以阅读它我发现他们为什么在重新提交后在url上转发我。第二次,jquery无法工作,因为在响应之后,我收到了新的html,jquery可以在按钮上绑定事件。作为解决方案,请更改JS代码
$('.container .form-comment').on('submit', '.send-comment', function(e) {
});
我的猜测是,在你的意思是什么的时候,on侦听器没有第二次注册?您能解释一下吗。@MaximR表单提交后的预期结果是什么?@MaximR如果出现错误,您将返回一个表单(包括我怀疑您注册的按钮)。在JS中注册eventlistener时,该表单不在DOM中。@MaximR您的意思是什么?symfony验证是确定的。它的工作,但是关于验证,它可以使用symfony验证,或者我应该手动来做吗?我的意思是如何使用这个验证{form_errors(form.domain)}或者类似的东西,并且在每个字段上显示用户要做什么样的错误。