CakePHP:验证用户超过13年

Cam*_*ron 2 cakephp

我的模型中有以下验证规则:

'dob' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Date of Birth is required'
            ),
            'age' => array(
                'rule' => array('comparison', '>=', 13),
                'message' => 'You must be over 13 years old'
            )
        )
Run Code Online (Sandbox Code Playgroud)

我想要实现的是验证用户已超过13岁......

日期创建如下:

<?php echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
                                        , 'dateFormat' => 'DMY'
                                        , 'minYear' => date('Y') - 110
                                        , 'maxYear' => date('Y') - 13)); ?>
Run Code Online (Sandbox Code Playgroud)

我怎么做呢?由于保存的数据是一个日期,而不是一个整数,所以我的比较将无法工作...在这里寻找最简单的解决方案,而无需回复插件或其他外部资产,如果可能的话只需要一些简单的代码.

谢谢.

编辑:所以根据下面的评论我添加:

public function checkDOB($check) {
        return strtotime($check['dob']) < strtotime();
    }
Run Code Online (Sandbox Code Playgroud)

但是我在strtotime中检查年龄是高于还是等于13?

Jus*_*ᚄᚒᚔ 5

在模型中创建自定义验证规则:

public function checkOver13($check) {
  $bday = strtotime($check['dob']);
  if (time() < strtotime('+13 years', $bday)) return false;
  return true;
}
Run Code Online (Sandbox Code Playgroud)

这使用了strtotime的一个简洁功能,可以让您轻松地在特定日期进行日期计算.

要使用规则:

'dob' => array(
  'age' => array(
    'rule' => 'checkOver13',
    'message' => 'You must be over 13 years old'
  )
)
Run Code Online (Sandbox Code Playgroud)