Cakephp 验证整数类型的生日

Cakephp 验证整数类型的生日,cakephp,Cakephp,这是我的信息模型中的验证我想验证生日但是的“生日”字段在DB中是int(8)。不幸的是,我没有修改MySQL数据库表的权限。现在如何验证它 生日示例:19800101 我试过了,但我想不出一个将生日转换为字符串的解决方案 var $validate = array( 'job_id' => array( 'rule' => 'notEmpty' , 'message' => 'Text here' ), 'sex_id'

这是我的信息模型中的验证我想验证生日但是的“生日”字段在DB中是int(8)。不幸的是,我没有修改MySQL数据库表的权限。现在如何验证它

生日示例:19800101

我试过了,但我想不出一个将生日转换为字符串的解决方案

var $validate = array(
    'job_id'        => array( 'rule' => 'notEmpty'              , 'message' => 'Text here' ),
    'sex_id'        => array( 'rule' => 'notEmpty'              , 'message' => 'Text here' ),
    'first_name'    => array( 'rule' => array( 'between', 1, 5 )    , 'message' => 'Text here' ),
    'last_name'     => array( 'rule' => array( 'between', 1, 5 )    , 'message' => '' ),
    //'birthday'        => array( 'rule' => 'some regex', 'message' => 'Text here' )
);

我相信只有使用正则表达式才能验证日期,包括闰年和一个月的总天数

您可以添加自己的验证方法。试试这个:

public $validate = array(
        'birthday' => array(
            'rule' => 'validateBirthday',
            'message' => 'Wrong date of birthday'
        )
);

public function validateBirthday($check) {
    $value = array_values($check);
    $value = $value[0];

    if (preg_match('/^(\d{4})(\d{2})(\d{2})$/', $value, $matches)) {
        return checkdate($matches[2], $matches[3], $matches[1]);
    } else {
        return false;
    }
}
更多信息:


是的,它可以工作。我使用
preg_match()
来分隔日、月和年。只需在模型中包含函数
validateBirthday
,然后更改
$validate
变量即可。