2011-09-28 88 views
0

我有一個註冊頁面,在此註冊頁面中,用戶需要填寫一些興趣,興趣值從名稱爲interest.php的模型中調用,所有值都通過並按預期保存。驗證規則不適用於CakePHP

用戶模型的關係是

var $hasAndBelongsToMany = array(
     'Interest' => array(
      'className' => 'Interest', 
      'joinTable' => 'users_interests', 
      'foreignKey' => 'user_id', 
      'associationForeignKey' => 'interest_id', 
      'unique' => true, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'finderQuery' => '', 
      'deleteQuery' => '', 
      'insertQuery' => '' 
     ) 

每當我加入interest.php小需要星形驗證規則後面會出現一個複選框,但它是不是在所有

<label for="InterestInterestId">Interests</label> 
<input type="hidden" value="" name="data[Interest][interest_id]"> 
<div class="checkbox"> 
<input id="InterestInterestId8" type="checkbox" value="8" name="data[Interest][interest_id][]"> 
<label for="InterestInterestId8">Interest 1</label> 
</div> 
<div class="checkbox"> 
<input id="InterestInterestId1" type="checkbox" value="1" name="data[Interest][interest_id][]"> 
<label for="InterestInterestId1">Interest2</label> 
</div> 
驗證

在我看來,我叫這樣的多個複選框

這是我的在interest.php

$this->validate = array(

      'interest_id' => array(
       'rule' => array(
        'equalTo', 
        '1' 
       ) , 
       'message' => __l('Please select some interests') 
      ) 
     ); 

驗證規則,我做得錯在這裏,或失去了一些東西,任何幫助將不勝感激!!!!

+0

我不知道這是原因,但不該」它是''equalTo'=>'1''? – bfavaretto

+0

我也試過 'interest_id'=>陣列( '規則'=>一個 'NotEmpty', '消息'=> __L( '必要'), 'allowEmpty'=>假 ), – Roland

+1

你也可以嘗試一些像這樣:http://stackoverflow.com/questions/2044102/habtm-form-validation-in-cakephp – bfavaretto

回答

0

找到與bfavaretto提供此鏈接HABTM form validation in CakePHP的幫助下回答

的問題是這個$this->User->Interest->set($this->data);

我忘了補充它

+0

將您自己的答案標記爲正確答案! – Wayne