Cakephp How to validate my D.O.B field so that the age will not be greater than the period of residence entered

只愿长相守 提交于 2019-12-08 04:52:48

问题


i have two fields in my form that should correspond to each other.If a users enters the date of birth,in the D.O.B field,he/she must not be allowed to enter a period of residence greater than the D.O.B

my two fields in the add.ctp are as follows

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

So now i don't know how i validate the two so that the user cannot enter a period of residence greater than the age.Even if it validates on submission i like it.


回答1:


You can create your own custom validation function in your model like so:

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


来源:https://stackoverflow.com/questions/11209968/cakephp-how-to-validate-my-d-o-b-field-so-that-the-age-will-not-be-greater-than

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!