CakePHP 2.2上的更改验证错误重定向

CakePHP 2.2上的更改验证错误重定向,cakephp,cakephp-2.0,cakephp-2.1,cakephp-2.2,Cakephp,Cakephp 2.0,Cakephp 2.1,Cakephp 2.2,我有一个与多个控制器交互的视图(帖子、评论、类别…) 如果注释不是空的,它将转到相同的操作(注释/添加),但在这种情况下,我手动将它们再次重定向到上一个视图,如下所示: $this->redirect(array('controller' => 'posts', 'action' => 'view', $this->request->data['Comment']['posts_id'], '#' => $this->Comment->id));

我有一个与多个控制器交互的视图(帖子、评论、类别…)

如果注释不是空的,它将转到相同的操作(注释/添加),但在这种情况下,我手动将它们再次重定向到上一个视图,如下所示:

$this->redirect(array('controller' => 'posts', 'action' => 'view', $this->request->data['Comment']['posts_id'], '#' => $this->Comment->id));
if ($this->request->is('ajax')) {
    $this->render('/Elements/comment_form');
    return;  // return the form only
} else {
    $this->render('/Posts/view'); // Your post view.ctp would have the comment form on it.
}
问题是,例如,当他们试图发布一条空注释时,注释的验证会将其重定向到comments/add(这是表单操作上的路由),但我不希望重定向这样做。我希望它返回到原始视图,并且我无论如何都不必在控制器或模型上更改它(据我所知)

有没有办法做到这一点


谢谢。

最简单的方法是通过AJAX提交表单。因此,在提交时,您不会刷新整个页面-您只需返回表单,并带有错误(如果有)

显然,对于没有Javascript的用户来说,这是行不通的。因此,您既可以不担心这些用户,也可以使comments控制器的add方法使用您想要的视图。大概是这样的:

$this->redirect(array('controller' => 'posts', 'action' => 'view', $this->request->data['Comment']['posts_id'], '#' => $this->Comment->id));
if ($this->request->is('ajax')) {
    $this->render('/Elements/comment_form');
    return;  // return the form only
} else {
    $this->render('/Posts/view'); // Your post view.ctp would have the comment form on it.
}
下面是一个我使用该方法的页面示例:尝试提交任何一个没有数据的表单

如果你需要更多信息,请告诉我