用于处理Model进入数据的验证
很多时候,我们在Model中处理数据的时候会一次传入很多参数,这时候可能需要对参数进行判断,是不是少参数了,参数的格式是否正确,如果每次都一大堆if判断的话会很麻烦,以前用过CI,它里面有对form表单提交的数据进行验证的类,然后按照我的习惯写了这个方法。
Validator.class.php<?php <br />
<br>
/**<br>
* 数据验证类<br>
* <br>
* @author Coeus <r.anerg><br>
*/<br>
<br>
namespace Lib\Util;<br>
<br>
class Validator {<br>
<br>
private $_vals;<br>
private $_rules;<br>
private $_errs;<br>
private $_error_code;<br>
<br>
public function set_rule($val, $rule, $err_code = -999) {<br>
$this->_vals[] = $val;<br>
$this->_rules[] = $rule;<br>
$this->_errs[] = $err_code;<br>
return $this;<br>
}<br>
<br>
public function ErrorCode() {<br>
return $this->_error_code;<br>
}<br>
<br>
private function reset() {<br>
$this->_vals = array();<br>
$this->_rules = array();<br>
$this->_errs = array();<br>
}<br>
<br>
public function run() {<br>
foreach ($this->_rules as $index => $rule) {<br>
$_errs = $this->_errs[$index];<br>
$errs = explode('|', $_errs);<br>
$methods = explode('|', $rule);<br>
if (!in_array('required', $methods) && ($this->_vals[$index] === false || $this->_vals[$index] === '' || $this->_vals[$index] === null))<br>
continue;<br>
foreach ($methods as $k => $method) {<br>
if (preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {<br>
$method = 'rule_' . $match[1];<br>
$param = $match[2];<br>
if (method_exists($this, $method) && $this->$method($index, $param) === false) {<br>
$this->_error_code = isset($errs[$k]) ? $errs[$k] : -999;<br>
$this->reset();<br>
return false;<br>
}<br>
} else {<br>
$method = 'rule_' . $method;<br>
if (method_exists($this, $method) && $this->$method($index) === false) {<br>
$this->_error_code = isset($errs[$k]) ? $errs[$k] : -999;<br>
$this->reset();<br>
return false;<br>
}<br>
}<br>
}<br>
}<br>
$this->reset();<br>
return true;<br>
}<br>
<br>
private function rule_trim($index) {<br>
$this->_vals[$index] = trim($this->_vals[$index]);<br>
}<br>
<br>
private function rule_required($index) {<br>
return $this->_vals[$index] !== '' && $this->_vals[$index] !== false && $this->_vals[$index] !== null ? true : false;<br>
}<br>
<br>
private function rule_regex_match($index, $regex) {<br>
return (bool) preg_match($regex, $this->_vals[$index]);<br>
}<br>
<br>
private function rule_matches($index, $match_val) {<br>
return (bool) $this->_vals[$index] === $match_val;<br>
}<br>
<br>
private function rule_integer($index) {<br>
return (bool) preg_match('/^[\-+]?[0-9]+$/', $this->_vals[$index]);<br>
}<br>
<br>
private function rule_mobile($index) {<br>
return (bool) preg_match('/^1[3|4|5|7|8]\d{9}$/', $this->_vals[$index]);<br>
}<br>
<br>
private function rule_min_len($index, $val) {<br>
return strlen($this->_vals[$index]) >= $val;<br>
}<br>
private function rule_max_len($index, $val) {<br>
return strlen($this->_vals[$index])
}<br>
<br>
}</r.anerg>
一般来说,我不喜欢把其他东西放进ThinkPHP文件夹下,所以有一个Lib文件夹,在Application文件夹下。然后配置config.php文件,修改如下'AUTOLOAD_NAMESPACE' => array('Lib' => APP_PATH . 'Lib'), //应用类库
这样就可以正常加载了。
另外,验证的配置文件我是单独放的,所以我的function.php文件里多了一个函数/**<br>
* 加载扩展的配置参数<br>
* @param type $file_name<br>
* @param type $is_common<br>
*/<br>
function load_ext_config($file_name, $is_common = true) {<br>
$file_path = $is_common ? COMMON_PATH . 'Conf/' . $file_name . '.php' : MODULE_PATH . 'Conf/' . $file_name;<br>
if (is_file($file_path)) {<br>
$_config[$file_name] = load_config($file_path);<br>
C($_config);<br>
}<br>
}
在Model中,具体使用示例如下: public function __construct() {<br>
load_ext_config('Valid');<br>
}<br>
<br>
/**<br>
* 创建订单<br>
* @param type $params<br>
* @return boolean<br>
*/<br>
public function create($params) {<br>
if ($this->_valid($params, __FUNCTION__) === false) {<br>
return false;<br>
}<br>
.............<br>
}<br>
/**<br>
* 字段格式及内容验证<br>
* @param type $params<br>
* @param type $method<br>
* @return boolean<br>
*/<br>
private function _valid($params, $method) {<br>
$Rules = C("VALID.ORDER");<br>
$Validator = new \Lib\Util\Validator();<br>
foreach ($Rules[$method] as $k => $v) {<br>
if ($Validator->set_rule($params[$k], $v['rule'], $v['err'])->run() === false) {<br>
$this->setErr($Validator->ErrorCode());<br>
return false;<br>
}<br>
}<br>
return true;<br>
}
数据验证的配置文件内容:<?php <br />
<br>
return array(<br>
'ORDER' => array(<br>
'create' => array(<br>
'order_time' => array(<br>
'rule' => 'integer',<br>
'err' => '-902001'<br>
),<br>
'order_fee' => array(<br>
'rule' => 'required|integer',<br>
'err' => '-903002|-903001'<br>
),<br>
'order_cost' => array(<br>
'rule' => 'required|integer',<br>
'err' => '-904002|-904001'<br>
),<br>
'order_status' => array(<br>
'rule' => 'integer',<br>
'err' => '-905001'<br>
),<br>
'order_channel' => array(<br>
'rule' => 'required|integer',<br>
'err' => '-906002|-906001'<br>
),<br>
'order_discount' => array(<br>
'rule' => 'integer',<br>
'err' => '-907001'<br>
),<br>
'uid' => array(<br>
'rule' => 'required|integer',<br>
'err' => '-106002|-106001'<br>
),<br>
),
AD:真正免费,域名+虚机+企业邮箱=0元