-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathWindEnableValidateModule.php
More file actions
135 lines (129 loc) · 3.43 KB
/
WindEnableValidateModule.php
File metadata and controls
135 lines (129 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* 表单验证基类
*
* 注入:验证器/异常处理器
*
* @author Qiong Wu <[email protected]>
* @copyright ©2003-2103 phpwind.com
* @license http://www.windframework.com
* @version $Id$
* @package base
*/
class WindEnableValidateModule {
/**
* 验证类
*
* @var string
*/
protected $_validatorClass = 'WIND:utility.WindValidator';
/**
* 错误处理action
*
* @var string
*/
protected $errorAction = '';
/**
* 验证类实例
*
* @var WindValidator
*/
private $_validator = null;
/**
* 验证中产生的错误信息
*
* @var array
*/
protected $_errors = array();
/**
* 验证中产生错误信息时使用的默认错误信息
*
* @var string
*/
private $_defaultMessage = 'the field validate fail.';
/**
* 返回验证中产生的错误信息
*
* @return array $_errors
*/
public function getErrors() {
return $this->_errors;
}
/**
* 返回验证出错时使用的错误errorAction
*
* errorAction的格式可以用/分割m,c,a三个部分:完整的方式为m/c/a
*
* @return string
*/
public function getErrorAction() {
return $this->errorAction;
}
/**
* 返回验证规则组成的数组
*
* 每一个验证规则都需要如下格式:
* <code>
* array(
* 'field' => 'name', //验证的字段名
* 'default' => 'test', //如果字段为空
* 'args' => array('args1', 'args2'), //validator验证方法接受的其他参数(必须在被验证参数的后面)
* 'validator' => 'isEmpty', //执行的验证方法(该方法必须在$_validatorClass指向的验证类中存在并可公开访问,并且该方法返回boolean类型)
* 'message' => 'name is empty', //验证失败时返回的错误信息
* )
* </code>
* 验证规则可以采用{@link WindUtility::buildValidateRule()}方法进行构造.
*
* @return array
*/
protected function validateRules() {
return array();
}
/**
* 验证方法
*
* @return void
*/
public function validate() {
$rules = $this->validateRules();
$methods = get_class_methods($this);
foreach ((array) $rules as $rule) {
$getMethod = 'get' . ucfirst($rule['field']);
$_input = in_array($getMethod, $methods) ? call_user_func(array($this, $getMethod)) : '';
if (true === $this->check($_input, $rule)) continue;
$setMethod = 'set' . ucfirst($rule['field']);
in_array($setMethod, $methods) && call_user_func_array(array($this, $setMethod), array($rule['default']));
}
}
/**
* 执行rule验证
*
* @param string $input 待验证的数据
* @param array $rule 数据验证的规则
* @return boolean 如果验证成功返回true,验证失败返回false
*/
private function check($input, $rule) {
$arg = (array) $rule['args'];
array_unshift($arg, $input);
if (call_user_func_array(array($this->getValidator(), $rule['validator']), $arg) !== false) return true;
if ($rule['default'] === null) {
$this->_errors[$rule['field']] = $rule['message'];
return true;
}
return false;
}
/**
* 返回验证器
*
* @return WindValidator
* @throws WindException 验证器创建失败抛出异常
*/
protected function getValidator() {
if ($this->_validator === null) {
$_className = Wind::import($this->_validatorClass);
$this->_validator = WindFactory::createInstance($_className);
if ($this->_validator === null) throw new WindException('validator', WindException::ERROR_RETURN_TYPE_ERROR);
}
return $this->_validator;
}
}