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
本文介绍了Cakephp 如何验证我的 D.O.B 字段,以便年龄不会大于输入的居住期限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表单中有两个字段应该相互对应.如果用户输入出生日期,则在 DOB 字段中,不得允许他/她输入大于 DOB 的居住期限>
我在add.ctp中的两个字段如下
echo $this->Form->input('DOB',array('label' => 'Date ofbirth*', 'minYear' => 1900, 'maxYear' =>; 2000));echo $this->Form->input('period_of_residence', array('label' =>'在津巴布韦的居住时间'));
所以现在我不知道我如何验证这两个以便用户不能输入大于年龄的居住时间.即使它在提交时验证我喜欢它.
解决方案
您可以像这样在模型中创建自己的自定义验证函数:
class MyModel 扩展 AppModel {公共 $validate = 数组('DOB' =>大批('规则' =>'checkDOB','消息' =>DOB 不能大于居住期限."));公共函数 checkDOB($check) {return strtotime($check['DOB']) 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屋!
查看全文