我需要返回表单验证,因为我在提交后丢失了它
i'm on News/view/30——查看报告详情----在该页中我们添加了注释表单:
<h2>Add comment</h2>
<?php
echo $this->Form->create("Comment", array(
'url' => array('controller' => 'Comments', 'action' => 'add')
));
echo $this->Form->label("name");
echo $this->Form->input("name", array("label" => false, "class" => "textfield"));
echo $this->Form->label("email");
echo $this->Form->input("email", array("label" => false, "class" => "textfield"));
echo $this->Form->label("text");
echo $this->Form->textarea("text", array("label" => false));
echo $this->Form->input("object_id", array("type" => "hidden", "value" => $data['News']['id']));
echo $this->Form->input("type", array("type" => "hidden", "value" => "news"));
echo $this->Html->Link("Add Comment", "#", array("class" => "add_button", "onclick" => "$('#CommentViewForm').submit()"));
echo $this->Form->end();
?>
表单正在提交注释控制器:
in comment/add:
$isSuccess = $this->Comment->save($this->request->data);
if ($isSuccess) {
$this->Session->setFlash('your Comments has been added successfully and pending admin aproval.thanks ', 'default', array(), 'good');
} else {
$this->Session->setFlash('Failed to add your comment: <br/>Fill all the required fileds <br/>type correct Email', 'default', array(), 'bad');
}
$this->redirect(array("controller" => "News", "action" => "view", $id));
我对名字、电子邮件和评论本身做了一些验证规则当用户输入有错误时,返回添加评论表单,但没有像往常一样显示错误
我希望你能很好的理解我,我等待你的帮助谢谢很多尝试将'error'=>true
添加到每个输入表单帮助器如echo $this->Form->input("name", array("label" => false, "class" => "textfield", 'error'=>true));
如果您不长时间使用CakePHP,您应该首先烘焙一些代码。然后,您将了解到,如果表单不验证,则不应该重定向:
if ($this->Comment->save($this->request->data)) {
$this->Session->setFlash('your Comments has been added successfully and pending admin aproval.thanks ', 'default', array(), 'good');
$this->redirect(array("controller" => "News", "action" => "view", $id));
} else {
$this->Session->setFlash('Failed to add your comment: <br/>Fill all the required fileds <br/>type correct Email', 'default', array(), 'bad');
// DO NOT redirect
}
烘焙模板将以这种方式烘焙你的控制器代码。这是一种干净透明的处理表单的方式。
重定向不会阻止您看到验证错误吗?即使有错误,您也将用户重定向到新页面(视图页面),因此他们永远不会看到任何验证错误消息。
尝试注释这一行:
//$this->redirect(array("controller" => "News", "action" => "view", $id));
或者以$isSuccess为true为条件。
回复:你的评论-问题是,如果你重定向离开带有表单的页面,你将不(容易)能够显示你的验证错误消息。最简单的方法是让表单提交给它自己的动作——听起来你是在把它提交给另一个控制器的动作。
我认为您需要在News控制器的视图操作中处理form->save,然后正如其他人所说,如果验证失败,不要重定向离开页面。
如果你重定向,你仍然会看到至少一次验证消息(来自会话Flash),但你会丢失填写的表单数据。
最简单的解决方案是移动保存到您的新闻->视图方法,如果您的新闻和评论模型相关联,只需使用可用的遍历来保存评论:
$isSuccess = $this->News->Comment->save($this->request->data);