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

Cakephp如何验证我的D.O.B字段,以便年龄不会大于输入的居住期限,cakephp,cakephp-1.3,cakephp-2.0,cakephp-2.1,Cakephp,Cakephp 1.3,Cakephp 2.0,Cakephp 2.1,我的表单中有两个字段应该相互对应。如果用户在D.O.B字段中输入出生日期,则他/她不得输入超过D.O.B的居住期限 add.ctp中的两个字段如下 echo $this->Form->input('DOB',array('label' => 'Date of birth*', 'minYear' => 1900, 'maxYear' => 2000)); echo $this->Form->input('period_of_residence'

我的表单中有两个字段应该相互对应。如果用户在D.O.B字段中输入出生日期,则他/她不得输入超过D.O.B的居住期限

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'));

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

您可以在模型中创建自己的自定义验证函数,如下所示:

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']);
    }
}
类MyModel扩展了AppModel{
public$validate=array(
'DOB'=>数组(
'规则'=>'检查日期',
'消息'=>'DOB不能大于居住期限。'
)
);
公共功能检查DOB($check){
返回strotime($check['DOB'])data['MyModel']['period\u of_residence']);
}
}