2009-08-01 26 views
0

我在CakePHP網站上關注blog tutorial,但驗證不起作用,我不明白爲什麼,因爲我正在使用博客教程代碼。在我的CakePHP中驗證似乎被禁用

,我會直接從我的文件反正報表...

模型

class Post extends AppModel 
{ 
    var $name = 'Post'; 

    var $validate = array(
     'title' => array(
      'rule' => 'notEmpty' 
     ), 
     'body' => array(
      'rule' => 'notEmpty' 
     ) 
    ); 
} 

控制器

class PostsController extends AppController { 
    var $name = 'Posts'; 

    function index() { 
     $this->set('posts', $this->Post->find('all')); 
    } 

    function view($id) { 
     $this->Post->id = $id; 
     $this->set('post', $this->Post->read()); 

    } 

    function add() { 
     if (!empty($this->data)) { 
      if ($this->Post->save($this->data)) { 
       $this->Session->setFlash('Your post has been saved.'); 
       $this->redirect(array('action' => 'index')); 
      } 
     } 
    } 
} 

VIEW(添加視圖)

<h1>Add Post</h1> 
<?php 
echo $form->create('Post'); 
echo $form->input('title'); 
echo $form->input('body', array('rows' => '3')); 
echo $form->end('Save Post'); 
?> 

當我到達/ posts/add頁面然後單擊以保存沒有任何輸入文本,它不會重新加載錯誤;它將空數據插入到數據庫中,然後使用消息「您的文章已保存」重定向到我。

爲什麼不驗證自己?我已閱讀docs,沒有必要致電validate(); save()就夠了。

對此有何想法?

回答

0

我發現了錯誤:我命名了大寫它們的模型文件。所以,我有一個Post.php而不是post.php

CakePHP沒有找到我的模型,所以它使用它的「生成」模型。