2012-03-29 34 views
0

这是我的InfoModel中的验证。 我想验证生日,但“生日”字段为int(8) in DB。不幸的是我没有修改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') 
); 

回答

1

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

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

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; 
    } 
} 

的更多信息:

+0

当生日是INT是否行得通?我需要转换$ check吗? – Emerald214 2012-03-29 10:12:04

+1

是的,它的工作原理。我使用'preg_match()'分隔日,月和年。只需在模型中包含函数'validateBirthday'并更改'$ validate'变量。 – 2012-03-29 10:14:49