2011-09-13 52 views
1
echo $this->Form->create('Comment', 
      array('url'=>array('controller' => 'comments', 'action' =>'add', $listposts['Post']['id'])) 
      ); 

    echo $this->Form->input('post_id',array('type'=>'hidden','style'=>'width:30%','value'=>$listposts['Post']['id'])); 
    echo $this->Form->input('name',array('style'=>'width:30%')); 
    echo $this->Form->input('email',array('style'=>'width:30%')); 
    echo $this->Form->input('body',array('rows'=>'5')); 

    echo $this->Form->end('Submit'); 

如果這三個字段中的任何一個爲空,它仍然將數據保存到表中。如何在一個輸入字段爲空時停止保存數據。但評論表的列不是null。帶有驗證問題的CakePHP提交表單

mysql> describe comments; 
+----------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+----------+--------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| post_id | int(11)  | NO | MUL | NULL |    | 
| name  | varchar(255) | NO |  | NULL |    | 
| email | varchar(255) | NO |  | NULL |    | 
| body  | varchar(500) | NO |  | NULL |    | 
| created | datetime  | YES |  | NULL |    | 
| modified | datetime  | YES |  | NULL |    | 
+----------+--------------+------+-----+---------+----------------+ 
7 rows in set (0.00 sec) 

Comment模型=>

<?php 
class Comment extends AppModel { 

    var $useTable='comments'; 
    var $belongsTo = array('Post'); 

} 

模型驗證,但它不顯示任何信息,但它不保存數據。

post<?php 
class Comment extends AppModel { 

    var $useTable='comments'; 
    var $belongsTo = array('Post'); 

    var $validate = array(
    'name' => array(
     'required' => true, 
     'rule' => 'notEmpty', 
     'allowEmpty' => false, 
     'message' => 'Enter Name.' 
    ), 
    'email' => array(
     'required' => true, 
     'rule' => 'notEmpty', 
     'allowEmpty' => false, 
     'message' => 'Enter Email.' 
    ), 
    'body' => array(
     'required' => true, 
     'rule' => 'notEmpty', 
     'allowEmpty' => false, 
     'message' => 'Enter Body.' 
    ) 
); 


} 
+0

參考這個鏈接http://book.cakephp.org/view/1143/Data-Validation – chetanspeed511987

+0

我編輯原來的職位或問題。 – shibly

回答

2

更改模型

<?php 
class Comment extends AppModel { 

var $useTable='comments'; 
var $belongsTo = array('Post'); 



var $validate = array(
'name' => array(
    'rule' => array('maxLength' , 80), 
    'required' => true, 
    'message' => 'Please enter your name' 
), 
'body' => array(
    'rule' => array('maxLength' , 80), 
    'required' => true, 
    'message' => 'Please enter your body' 
), 

'email' => array(
    'rule' => 'email', 
    'required' => true, 
    'message' => 'Please enter a valid email address.' 
), 

); 

} 

    ?> 
+0

我得到了錯誤,解析錯誤:語法錯誤,意想不到的T_STRING,期待')'行'規則'=>數組('maxLength',80), – shibly

+0

我已編輯原始帖子/問題與驗證。但是,如果任何輸入字段爲空,它仍然保持在同一頁面中。但它不會保存任何數據,因爲一個字段爲空。但它沒有顯示任何消息。 – shibly

+0

爲desplay消息,請參閱此鏈接http://stackoverflow.com/questions/6529023/structuring-a-cakephp-registration-page/6544913#6544913 – chetanspeed511987

4

在您的控制器添加這些行,然後檢查什麼是錯誤顯示..我想切切實實的它會正常工作。與空字段ü提交表單顯示出錯

$this->Comment->set($this->data); 

if ($this->Comment->validates()) 
{ 
     if ($this->Comment->save($this->data)) 
     { 
     $this->Session->setFlash('Comment saved ', 'default')); 
     } 
     else 
     { 
      var_dump($this->Comment->invalidFields()); 
      //OR 
      $this->Comment->validationErrors(); 
     } 


} 
else 
{ 
    var_dump($this->Comment->invalidFields()); 
    //OR 
    $this->Comment->validationErrors(); 


}