2011-09-28 89 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