2012-06-26 11 views
1

我的表單中有兩個字段應該相互對應。如果用戶輸入出生日期,在DOB現場,他/她一定不能被允許進入居住一段時間比DOB更大的Cakephp如何驗證我的DOB字段,以便年齡不會大於輸入居住期間

我的兩個在add.ctp字段如下所以現在如下

echo $this->Form->input('DOB',array('label' => 'Date of birth*', 'minYear' => 1900, 'maxYear' => 2000)); 


echo $this->Form->input('period_of_residence', array('label' =>'Period of residence in Zimbabwe')); 

我不知道我驗證兩者,以便用戶不能進入大於年齡的居住期。即使它在提交時驗證我也喜歡。

回答

3

您可以在模型中創建自己的自定義驗證函數,像這樣:

class MyModel extends AppModel { 

    public $validate = array(
     'DOB' => array(
      'rule' => 'checkDOB', 
      'message' => 'DOB cannot be greater than period of residence.' 
     ) 
    ); 

    public function checkDOB($check) { 
     return strtotime($check['DOB']) < strtotime($this->data['MyModel']['period_of_residence']); 
    } 
}