我想“保存”模型以运行验证,但不将其写入数据库


I want to "save" the model to run the validations, but not write it into the database

我一直在寻找如何做到这一点的解决方案,但我最接近的解决方案是在你的模型中,有

public function beforeSave() {
    parent::beforeSave();
    if(!isset($this->data["Model"]["confirm"])) {
        return false;
    }
   }

在控制器中只需执行以下操作:

        $this->Model->save($this->request->data);
        if($this->Model->validates()) {
            $this->render("confirm");
        } else {
            $this->Session->setFlash(__('The model could not be saved. Please, try again.'));
            // now we can render
            $this->render("index");
        }

这种方法的问题在于它会显示两次错误消息,有没有更好的方法可以做到这一点?或者我应该以黑客的方式执行此操作,并找到一种方法来删除显示两次的错误消息。

我应该更深入地研究文档:答案在于 http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html

感谢@dm03514指点我查看文档而不是 API(我已经阅读了几天但没有找到真正的答案)!