Skip to content

Commit f809ccc

Browse files
committed
update, add new validator: enum. add test
1 parent c3a059e commit f809ccc

File tree

7 files changed

+192
-163
lines changed

7 files changed

+192
-163
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ public function get(string $key, $default = null)
499499

500500
## 内置的验证器
501501

502+
> `/` 分隔的验证器,表明功能是一样的,只是有不同的别名
503+
502504
验证器 | 说明 | 规则示例
503505
----------|-------------|------------
504506
`int/integer` | 验证是否是 int | `['userId', 'int']`
@@ -520,7 +522,7 @@ public function get(string $key, $default = null)
520522
`max` | 最大边界值验证 | `['title', 'max', 40]`
521523
`size/range/between` | 验证大小范围, 可以支持验证 `int`, `string`, `array` 数据类型 | `['tagId', 'size', 'min'=>4, 'max'=>567]`
522524
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | `['username', 'length', 'min' => 5, 'max' => 20]`
523-
`in` | 枚举验证 | `['status', 'in', [1,2,3]`
525+
`in/enum` | 枚举验证 | `['status', 'in', [1,2,3]`
524526
`notIn` | 枚举验证 | `['status', 'notIn', [4,5,6]]`
525527
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 0]`
526528
`compare/same/equal` | 字段值比较 | `['passwd', 'compare', 'repasswd']`

src/FieldValidation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ protected function collectRules()
7878
yield $field => $rule;
7979
}
8080
}
81-
8281
}
8382
}
8483

@@ -102,6 +101,7 @@ protected function parseRule($rule, $row)
102101

103102
switch ($name) {
104103
case 'in':
104+
case 'enum':
105105
case 'ontIn':
106106
$row[] = array_map('trim', explode(',', $args));
107107
break;

src/Utils/ErrorInformationTrait.php

Lines changed: 0 additions & 156 deletions
This file was deleted.

src/Utils/ErrorMessageTrait.php

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ trait ErrorMessageTrait
4242
'size' => [
4343
'{attr} size validation is not through!',
4444
'{attr} must be an integer/string/array and minimum value/length is {min}',
45-
'{attr} must be an integer/string/array and value/length range {min} ~ {max}',
45+
// '{attr} must be an integer/string/array and value/length range {min} ~ {max}',
46+
'{attr} must be in the range {min} ~ {max}',
4647
],
4748
'range' => [
4849
'{attr} range validation is not through!',
@@ -57,6 +58,7 @@ trait ErrorMessageTrait
5758
'min' => '{attr} minimum boundary is {value0}',
5859
'max' => '{attr} maximum boundary is {value0}',
5960
'in' => '{attr} must in ({value0})',
61+
'enum' => '{attr} must in ({value0})',
6062
'notIn' => '{attr} cannot in ({value0})',
6163
'string' => [
6264
'{attr} must be a string',
@@ -86,6 +88,158 @@ trait ErrorMessageTrait
8688
*/
8789
private $_translates = [];
8890

91+
/**
92+
* 保存所有的验证错误信息
93+
* @var array[]
94+
* [
95+
* [ field => errorMessage1 ],
96+
* [ field => errorMessage2 ],
97+
* [ field2 => errorMessage3 ]
98+
* ]
99+
*/
100+
private $_errors = [];
101+
102+
/**
103+
* Whether there is error stop validation 是否出现验证失败就立即停止验证
104+
* True -- 出现一个验证失败即停止验证,并退出
105+
* False -- 全部验证并将错误信息保存到 {@see $_errors}
106+
* @var boolean
107+
*/
108+
private $_stopOnError = true;
109+
110+
/*******************************************************************************
111+
* Errors Information
112+
******************************************************************************/
113+
114+
/**
115+
* @return $this
116+
*/
117+
public function clearErrors()
118+
{
119+
$this->_errors = [];
120+
121+
return $this;
122+
}
123+
124+
/**
125+
* 是否有错误
126+
* @return boolean
127+
*/
128+
public function hasError(): bool
129+
{
130+
return $this->isFail();
131+
}
132+
133+
/**
134+
* @return bool
135+
*/
136+
public function isFail(): bool
137+
{
138+
return \count($this->_errors) > 0;
139+
}
140+
141+
/**
142+
* @return bool
143+
*/
144+
public function fail(): bool
145+
{
146+
return $this->isFail();
147+
}
148+
149+
/**
150+
* @return bool
151+
*/
152+
public function failed(): bool
153+
{
154+
return $this->isFail();
155+
}
156+
157+
/**
158+
* @return bool
159+
*/
160+
public function passed(): bool
161+
{
162+
return !$this->isFail();
163+
}
164+
165+
/**
166+
* @return bool
167+
*/
168+
public function isPassed(): bool
169+
{
170+
return !$this->isFail();
171+
}
172+
173+
/**
174+
* @param string $attr
175+
* @param string $msg
176+
*/
177+
public function addError(string $attr, string $msg)
178+
{
179+
$this->_errors[] = [$attr => $msg];
180+
}
181+
182+
/**
183+
* @return array
184+
*/
185+
public function getErrors(): array
186+
{
187+
return $this->_errors;
188+
}
189+
190+
/**
191+
* 得到第一个错误信息
192+
* @author inhere
193+
* @param bool $onlyMsg
194+
* @return array|string
195+
*/
196+
public function firstError($onlyMsg = true)
197+
{
198+
$e = $this->_errors;
199+
$first = array_shift($e);
200+
201+
return $onlyMsg ? array_values($first)[0] : $first;
202+
}
203+
204+
/**
205+
* 得到最后一个错误信息
206+
* @author inhere
207+
* @param bool $onlyMsg
208+
* @return array|string
209+
*/
210+
public function lastError($onlyMsg = true)
211+
{
212+
$e = $this->_errors;
213+
$last = array_pop($e);
214+
215+
return $onlyMsg ? array_values($last)[0] : $last;
216+
}
217+
218+
/**
219+
* @param bool|null $stopOnError
220+
* @return $this
221+
*/
222+
public function setStopOnError($stopOnError = null)
223+
{
224+
if (null !== $stopOnError) {
225+
$this->_stopOnError = (bool)$stopOnError;
226+
}
227+
228+
return $this;
229+
}
230+
231+
/**
232+
* @return bool
233+
*/
234+
public function isStopOnError(): bool
235+
{
236+
return $this->_stopOnError;
237+
}
238+
239+
/*******************************************************************************
240+
* Error Messages
241+
******************************************************************************/
242+
89243
/**
90244
* @return array
91245
*/

src/ValidationTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use Inhere\Validate\Utils\Helper;
1212
use Inhere\Validate\Utils\ErrorMessageTrait;
13-
use Inhere\Validate\Utils\ErrorInformationTrait;
1413
use Inhere\Validate\Utils\RequiredValidatorsTrait;
1514

1615
/**
@@ -20,7 +19,7 @@
2019
*/
2120
trait ValidationTrait
2221
{
23-
use ErrorInformationTrait, ErrorMessageTrait, RequiredValidatorsTrait;
22+
use ErrorMessageTrait, RequiredValidatorsTrait;
2423

2524
/**
2625
* custom add's validator by addValidator()

0 commit comments

Comments
 (0)