Showing
4 changed files
with
139 additions
and
29 deletions
@@ -17,7 +17,11 @@ class Language | @@ -17,7 +17,11 @@ class Language | ||
17 | "alpha_dash" => "{attribute} 只能由字母、数字和斜杠组成.", | 17 | "alpha_dash" => "{attribute} 只能由字母、数字和斜杠组成.", |
18 | "alpha_num" => "{attribute} 只能由字母和数字组成.", | 18 | "alpha_num" => "{attribute} 只能由字母和数字组成.", |
19 | "array" => "{attribute} 必须是一个数组.", | 19 | "array" => "{attribute} 必须是一个数组.", |
20 | - "between" => "{attribute} 必须介于 {min} - {max} (个字符\个单元)之间.", | 20 | + "between" => [ |
21 | + "numeric" => "{attribute} 必须介于 {min} - {max} 之间.", | ||
22 | + "string" => "{attribute} 必须介于 {min} - {max} 个字符之间.", | ||
23 | + "array" => "{attribute} 必须只有 {min} - {max} 个单元.", | ||
24 | + ], | ||
21 | "boolean" => "{attribute} 必须为布尔值.", | 25 | "boolean" => "{attribute} 必须为布尔值.", |
22 | "confirmed" => "{attribute} 两次输入不一致.", | 26 | "confirmed" => "{attribute} 两次输入不一致.", |
23 | "date" => "{attribute} 不是一个有效的日期.", | 27 | "date" => "{attribute} 不是一个有效的日期.", |
@@ -29,13 +33,26 @@ class Language | @@ -29,13 +33,26 @@ class Language | ||
29 | "integer" => " {attribute} 必须是整数.", | 33 | "integer" => " {attribute} 必须是整数.", |
30 | 'string' => '{attribute} 必须是字符.', | 34 | 'string' => '{attribute} 必须是字符.', |
31 | "ip" => "{attribute} 必须是有效的 IP 地址.", | 35 | "ip" => "{attribute} 必须是有效的 IP 地址.", |
32 | - "max" => "{attribute} 不能大于 {max} (个字符\个单元).", | ||
33 | - "min" => "{attribute} 至少有 {min} (个字符\个单元).", | 36 | + "max" => [ |
37 | + "numeric" => "{attribute} 不能大于 {max}.", | ||
38 | + "string" => "{attribute} 不能大于 {max} 个字符.", | ||
39 | + "array" => "{attribute} 最多只有 {max} 个单元.", | ||
40 | + ], | ||
41 | + "min" => [ | ||
42 | + "numeric" => "{attribute} 必须大于等于 {min}.", | ||
43 | + "string" => "{attribute} 至少为 {min} 个字符.", | ||
44 | + "array" => "{attribute} 至少有 {min} 个单元.", | ||
45 | + ], | ||
34 | "numeric" => "{attribute} 必须是一个数字.", | 46 | "numeric" => "{attribute} 必须是一个数字.", |
35 | "regex" => "{attribute} 格式不正确.", | 47 | "regex" => "{attribute} 格式不正确.", |
36 | "required" => "{attribute} 不能为空.", | 48 | "required" => "{attribute} 不能为空.", |
37 | "same" => "{attribute} 和 {other} 必须相同.", | 49 | "same" => "{attribute} 和 {other} 必须相同.", |
38 | - "size" => "{attribute} 必须是 {size} (大小\个字符\个单元).", | 50 | + "size" => [ |
51 | + "numeric" => "{attribute} 大小必须为 {size}.", | ||
52 | + "string" => "{attribute} 必须是 {size} 个字符.", | ||
53 | + "array" => "{attribute} 必须为 {size} 个单元.", | ||
54 | + ], | ||
39 | "active_url" => "{attribute} 不是一个有效的网址.", | 55 | "active_url" => "{attribute} 不是一个有效的网址.", |
56 | + "url" => "{attribute} 格式不正确。", | ||
40 | ]; | 57 | ]; |
41 | } | 58 | } |
@@ -13,36 +13,69 @@ class Messages | @@ -13,36 +13,69 @@ class Messages | ||
13 | { | 13 | { |
14 | private $messages = array(); | 14 | private $messages = array(); |
15 | 15 | ||
16 | - private $messagesAttribute = array(); | 16 | + private $customMessages = array(); |
17 | 17 | ||
18 | private $defaultMessagesLanguage = array(); | 18 | private $defaultMessagesLanguage = array(); |
19 | 19 | ||
20 | - public function __construct(array $messagesAttribute = array()) | 20 | + public function __construct(array $customMessages = array()) |
21 | { | 21 | { |
22 | $this->defaultMessagesLanguage = Language::$messages; | 22 | $this->defaultMessagesLanguage = Language::$messages; |
23 | - $this->messagesAttribute = $messagesAttribute; | 23 | + $this->customMessages = $customMessages; |
24 | } | 24 | } |
25 | 25 | ||
26 | - public function messagesAttribute(array $messagesAttribute = array()) | 26 | + public function customMessages(array $customMessages = array()) |
27 | { | 27 | { |
28 | - $this->messagesAttribute = $messagesAttribute; | 28 | + $this->customMessages = $customMessages; |
29 | } | 29 | } |
30 | 30 | ||
31 | + /** | ||
32 | + * 获取所有错误 | ||
33 | + * @return array | ||
34 | + */ | ||
31 | public function all() | 35 | public function all() |
32 | { | 36 | { |
33 | - return $this->messages; | 37 | + $all = array(); |
38 | + foreach ($this->messages as $key => $messages) { | ||
39 | + $all = array_merge($all, $messages); | ||
40 | + } | ||
41 | + return $all; | ||
34 | } | 42 | } |
35 | 43 | ||
36 | public function setMessages($key, $val) | 44 | public function setMessages($key, $val) |
37 | { | 45 | { |
38 | - $this->messages[$key] = $val; | 46 | + $this->messages[$key][] = $val; |
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * 查看所有字段的所有错误信息 | ||
51 | + * @param $key | ||
52 | + * @return array | ||
53 | + */ | ||
54 | + public function get($key) | ||
55 | + { | ||
56 | + if (isset($this->messages[$key])) { | ||
57 | + return $this->messages[$key]; | ||
58 | + } | ||
59 | + return array(); | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * 判断是否有错误信息 | ||
64 | + * @return bool | ||
65 | + */ | ||
66 | + public function has() | ||
67 | + { | ||
68 | + return isset($this->messages[$key]); | ||
39 | } | 69 | } |
40 | 70 | ||
41 | - public function getMessages($key) | 71 | + public function getMessages($key, $type = null) |
42 | { | 72 | { |
43 | - if (isset($this->messagesAttribute[$key])) { | ||
44 | - return $this->messagesAttribute[$key]; | 73 | + if (isset($this->customMessages[$key])) { |
74 | + return $this->customMessages[$key]; | ||
45 | } elseif (array_key_exists($key, $this->defaultMessagesLanguage)) { | 75 | } elseif (array_key_exists($key, $this->defaultMessagesLanguage)) { |
76 | + if (isset($this->defaultMessagesLanguage[$key][$type])) { | ||
77 | + return $this->defaultMessagesLanguage[$key][$type]; | ||
78 | + } | ||
46 | return $this->defaultMessagesLanguage[$key]; | 79 | return $this->defaultMessagesLanguage[$key]; |
47 | } | 80 | } |
48 | return ''; | 81 | return ''; |
@@ -11,6 +11,18 @@ | @@ -11,6 +11,18 @@ | ||
11 | ); | 11 | ); |
12 | 字段值通过 PHP 函数 checkdnsrr 来验证是否为一个有效的网址。 | 12 | 字段值通过 PHP 函数 checkdnsrr 来验证是否为一个有效的网址。 |
13 | ``` | 13 | ``` |
14 | +##url | ||
15 | +```php | ||
16 | + Validator::make( | ||
17 | + ['url'=>'active_url'] | ||
18 | + ) | ||
19 | + | ||
20 | + Validator::make( | ||
21 | + ['url_str' => 'http://www.baidu.com'], | ||
22 | + ['url_str' => 'url'] | ||
23 | + ); | ||
24 | + 网址格式不正确。 | ||
25 | +``` | ||
14 | ##after | 26 | ##after |
15 | ```php | 27 | ```php |
16 | Validator::make( | 28 | Validator::make( |
@@ -304,4 +316,22 @@ | @@ -304,4 +316,22 @@ | ||
304 | ['date_time' => 'size:1'] | 316 | ['date_time' => 'size:1'] |
305 | ); | 317 | ); |
306 | 字段值的尺寸需符合给定 value 值。对于字串来说,value 为需符合的字串长度。对于数字来说,value 为需符合的整数值 | 318 | 字段值的尺寸需符合给定 value 值。对于字串来说,value 为需符合的字串长度。对于数字来说,value 为需符合的整数值 |
319 | +``` | ||
320 | +##code | ||
321 | +```php | ||
322 | + 判断是否有错误 | ||
323 | + if ($validator->fails() == true) { | ||
324 | + | ||
325 | + } | ||
326 | + 判断是否通过 | ||
327 | + if ($validator->passes() == true) { | ||
328 | + | ||
329 | + } | ||
330 | + | ||
331 | + 获取所有错误 | ||
332 | + foreach ($validator->messages()->all() as $key => $val) { | ||
333 | + | ||
334 | + } | ||
335 | + 获取指定错误 | ||
336 | + $validator->messages()->get('email'); | ||
307 | ``` | 337 | ``` |
@@ -16,16 +16,14 @@ class Validator | @@ -16,16 +16,14 @@ class Validator | ||
16 | 16 | ||
17 | private $data; | 17 | private $data; |
18 | 18 | ||
19 | - private $rules; | ||
20 | - | ||
21 | - private $failedRules; | 19 | + protected $failedRules = array(); |
22 | 20 | ||
23 | - protected $sizeRules = array('Size', 'Between', 'Min', 'Max'); | 21 | + private $rules; |
24 | 22 | ||
25 | protected $numericRules = array('Numeric', 'Integer'); | 23 | protected $numericRules = array('Numeric', 'Integer'); |
26 | 24 | ||
27 | protected $implicitRules = array( | 25 | protected $implicitRules = array( |
28 | - 'Required', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll', 'RequiredIf', 'Accepted' | 26 | + 'Required' |
29 | ); | 27 | ); |
30 | 28 | ||
31 | protected $messagesRules = array( | 29 | protected $messagesRules = array( |
@@ -33,7 +31,7 @@ class Validator | @@ -33,7 +31,7 @@ class Validator | ||
33 | 'alphanum' => 'alpha_num', | 31 | 'alphanum' => 'alpha_num', |
34 | 'digitsbetween' => 'digits_between', | 32 | 'digitsbetween' => 'digits_between', |
35 | 'notin' => 'not_in', | 33 | 'notin' => 'not_in', |
36 | - 'activeurl'=>'active_url' | 34 | + 'activeurl' => 'active_url' |
37 | ); | 35 | ); |
38 | 36 | ||
39 | /** | 37 | /** |
@@ -41,16 +39,21 @@ class Validator | @@ -41,16 +39,21 @@ class Validator | ||
41 | */ | 39 | */ |
42 | private $messages; | 40 | private $messages; |
43 | 41 | ||
44 | - public function __construct(array $data, array $rules, array $messagesAttribute = array()) | 42 | + public function __construct(array $data, array $rules, array $customMessages = array()) |
45 | { | 43 | { |
46 | - $this->messages = new Messages($messagesAttribute); | 44 | + $this->messages = new Messages($customMessages); |
47 | $this->data = $this->parseData($data); | 45 | $this->data = $this->parseData($data); |
48 | $this->rules = $this->explodeRules($rules); | 46 | $this->rules = $this->explodeRules($rules); |
49 | } | 47 | } |
50 | 48 | ||
51 | - public function messagesAttribute(array $messagesAttribute = array()) | 49 | + /** |
50 | + * 自定义错误信息 | ||
51 | + * @param array $customMessages | ||
52 | + * @return $this | ||
53 | + */ | ||
54 | + public function customMessages(array $customMessages = array()) | ||
52 | { | 55 | { |
53 | - $this->messages->messagesAttribute($messagesAttribute); | 56 | + $this->messages->customMessages($customMessages); |
54 | return $this; | 57 | return $this; |
55 | } | 58 | } |
56 | 59 | ||
@@ -83,6 +86,10 @@ class Validator | @@ -83,6 +86,10 @@ class Validator | ||
83 | return $rules; | 86 | return $rules; |
84 | } | 87 | } |
85 | 88 | ||
89 | + /** | ||
90 | + * 通过 | ||
91 | + * @return bool | ||
92 | + */ | ||
86 | public function passes() | 93 | public function passes() |
87 | { | 94 | { |
88 | foreach ($this->rules as $attribute => $rules) { | 95 | foreach ($this->rules as $attribute => $rules) { |
@@ -109,19 +116,32 @@ class Validator | @@ -109,19 +116,32 @@ class Validator | ||
109 | $validatable = $this->isValidatable($rule, $attribute, $value); | 116 | $validatable = $this->isValidatable($rule, $attribute, $value); |
110 | $method = "validate{$rule}"; | 117 | $method = "validate{$rule}"; |
111 | if ($validatable && !$this->$method($attribute, $value, $parameters, $this)) { | 118 | if ($validatable && !$this->$method($attribute, $value, $parameters, $this)) { |
112 | - $this->addError($attribute, $rule, $parameters); | 119 | + $this->addError($attribute, $rule, $parameters, $value); |
120 | + $this->failedRules[$attribute][$rule] = $parameters; | ||
113 | } | 121 | } |
114 | } | 122 | } |
115 | 123 | ||
116 | - protected function addError($attribute, $rule, $parameters) | 124 | + protected function addError($attribute, $rule, $parameters, $value) |
117 | { | 125 | { |
118 | $messagesKey = strtolower($rule); | 126 | $messagesKey = strtolower($rule); |
119 | if (isset($this->messagesRules[$messagesKey])) { | 127 | if (isset($this->messagesRules[$messagesKey])) { |
120 | $messagesKey = $this->messagesRules[$messagesKey]; | 128 | $messagesKey = $this->messagesRules[$messagesKey]; |
121 | } | 129 | } |
122 | - $message = $this->messages->getMessages($messagesKey); | 130 | + $message = $this->messages->getMessages($messagesKey, $this->getValueType($value)); |
123 | $message = $this->doReplacements($message, $attribute, $rule, $parameters); | 131 | $message = $this->doReplacements($message, $attribute, $rule, $parameters); |
124 | - $this->messages->setMessages(strtolower($attribute . '.' . $rule), $message); | 132 | + $this->messages->setMessages(strtolower($attribute), $message); |
133 | + } | ||
134 | + | ||
135 | + protected function getValueType($value) | ||
136 | + { | ||
137 | + if (is_numeric($value)) { | ||
138 | + return 'numeric'; | ||
139 | + } elseif (is_array($value)) { | ||
140 | + return 'array'; | ||
141 | + } elseif (is_string($value)) { | ||
142 | + return 'string'; | ||
143 | + } | ||
144 | + return null; | ||
125 | } | 145 | } |
126 | 146 | ||
127 | protected function getAttributeType($attribute) | 147 | protected function getAttributeType($attribute) |
@@ -299,6 +319,16 @@ class Validator | @@ -299,6 +319,16 @@ class Validator | ||
299 | return checkdnsrr($url); | 319 | return checkdnsrr($url); |
300 | } | 320 | } |
301 | 321 | ||
322 | + protected function validateUrl($attribute, $value) | ||
323 | + { | ||
324 | + return filter_var($value, FILTER_VALIDATE_URL) !== false; | ||
325 | + } | ||
326 | + | ||
327 | + protected function replaceUrl($message, $attribute, $rule, $parameters) | ||
328 | + { | ||
329 | + return str_replace('{attribute}', $attribute, $message); | ||
330 | + } | ||
331 | + | ||
302 | protected function validateAfter($attribute, $value) | 332 | protected function validateAfter($attribute, $value) |
303 | { | 333 | { |
304 | return strtotime($value) ? true : false; | 334 | return strtotime($value) ? true : false; |
@@ -536,6 +566,6 @@ class Validator | @@ -536,6 +566,6 @@ class Validator | ||
536 | 566 | ||
537 | public function messages() | 567 | public function messages() |
538 | { | 568 | { |
539 | - return $this->messages->all(); | 569 | + return $this->messages; |
540 | } | 570 | } |
541 | } | 571 | } |
-
Please register or login to post a comment