Error handling CakePHP将用户发布的值返回表单时的错误处理?
通常,处理错误的常见方法是简单地执行重定向,如下所示:Error handling CakePHP将用户发布的值返回表单时的错误处理?,error-handling,cakephp-2.0,Error Handling,Cakephp 2.0,通常,处理错误的常见方法是简单地执行重定向,如下所示: if ({something that is error}) { $this->Session->setFlash(__('Error message'), true); $this->redirect(array('controller' => 'some_controller', 'action' => 'some_action')); } 但是,在if($this->request->i
if ({something that is error}) {
$this->Session->setFlash(__('Error message'), true);
$this->redirect(array('controller' => 'some_controller', 'action' => 'some_action'));
}
但是,在if($this->request->is('post'))过程中会发生多个检查{
方法的一节。如果任何检查失败,我想退出其余的检查,将用户返回表单,并使用他们以前输入的所有设置,这样他们就不必再次填写表单。我将如何完成
public function custom_method() {
// get some information here
if ($this->request->is('post')) {
// do_check
if (!do_check) {
// set flash
// log error
// don't run the rest of the checks - go to end of the if ($this->request->is('post'))
}
// do other check
if (!do_other_check) {
// set flash
// log error
// don't run the rest of the checks - go to end of the if ($this->request->is('post'))
}
// do database update
}
// do other stuff here
// then it goes to render view
}
在与我的一位好朋友(谢谢@utoxin)交谈后,我想出了一个很好的解决方案!他提到模型验证,帮助我朝着正确的方向前进 以下是我将如何做到这一点:
public function prepare() {
// do some stuff here
if ($this->request->is('post')) {
$postValid = true;
if ($postValid and !some_check) {
$this->log('Log error here', 'error');
$this->Model->invalidate('form_field', 'No valid installation file was found for this version.');
$postValid = false;
}
if($postValid){
// no error, do redirect, continue successfully
}
}
// do other stuff here
// show form
}