Cakephp如何验证我的D.O.B字段,使年龄不会大于输入的居住期 [英] Cakephp How to validate my D.O.B field so that the age will not be greater than the period of residence entered

查看:144
本文介绍了Cakephp如何验证我的D.O.B字段,使年龄不会大于输入的居住期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有两个字段应该彼此对应。如果用户输入出生日期,在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'=>'在津巴布韦的居住期限'));

现在我不知道如何验证这两个,以便用户不能输入一个期间

解决方案

您可以创建自己的自定义验证函数你的模型如下:

  class MyModel extends AppModel {

public $ validate = array $ b'DOB'=> array(
'rule'=>'checkDOB',
'message'=>'DOB不能大于居住期限'

);

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


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.

解决方案

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

这篇关于Cakephp如何验证我的D.O.B字段,使年龄不会大于输入的居住期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆