Cakephp如何验证我的D.O.B字段,以便年龄不会大于居住时间

时间:2012-06-26 14:48:08

标签: cakephp cakephp-1.3 cakephp-2.0 cakephp-2.1

我的表格中有两个字段应该相互对应。如果用户输入出生日期,在DOB字段中,他/她不得进入大于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'));

所以现在我不知道我如何验证这两个,以便用户不能进入大于年龄的居住时期。即使它在提交时验证我喜欢它。

1 个答案:

答案 0 :(得分: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']);
    }
}