Skip to content

Commit b36f9b8

Browse files
committed
some update, support field to many rule validate.
1 parent ffcebfd commit b36f9b8

File tree

5 files changed

+432
-396
lines changed

5 files changed

+432
-396
lines changed

src/FieldValidation.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected function parseRule($rule, $row)
8888
}
8989

9090
list($name, $args) = explode(':', $rule, 2);
91+
$args = trim($args, ', ');
9192
$row[0] = $name;
9293

9394
switch ($name) {
@@ -96,8 +97,19 @@ protected function parseRule($rule, $row)
9697
$row[] = array_map('trim', explode(',', $args));
9798
break;
9899

100+
case 'size':
101+
case 'range':
102+
case 'string':
103+
case 'between':
104+
if (strpos($args, ',')) {
105+
list($row['min'], $row['max']) = array_map('trim', explode(',', $args, 2));
106+
} else {
107+
$row['min'] = $args;
108+
}
109+
break;
99110
default:
100-
$row[] = $args;
111+
$args = strpos($args, ',') ? array_map('trim', explode(',', $args)) : [$args];
112+
$row = array_merge($row, $args);
101113
break;
102114
}
103115

src/Utils/ErrorInformationTrait.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-11-17
6+
* Time: 11:26
7+
*/
8+
9+
namespace Inhere\Validate\Utils;
10+
11+
/**
12+
* trait ErrorInformationTrait
13+
* @package Inhere\Validate\Utils
14+
*/
15+
trait ErrorInformationTrait
16+
{
17+
/**
18+
* 保存所有的验证错误信息
19+
* @var array[]
20+
* [
21+
* [ field => errorMessage1 ],
22+
* [ field => errorMessage2 ],
23+
* [ field2 => errorMessage3 ]
24+
* ]
25+
*/
26+
private $_errors = [];
27+
28+
/**
29+
* Whether there is error stop validation 是否出现验证失败就立即停止验证
30+
* True -- 出现一个验证失败即停止验证,并退出
31+
* False -- 全部验证并将错误信息保存到 {@see $_errors}
32+
* @var boolean
33+
*/
34+
private $_stopOnError = true;
35+
36+
/*******************************************************************************
37+
* Errors
38+
******************************************************************************/
39+
40+
/**
41+
* @return $this
42+
*/
43+
public function clearErrors()
44+
{
45+
$this->_errors = [];
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* 是否有错误
52+
* @return boolean
53+
*/
54+
public function hasError(): bool
55+
{
56+
return $this->isFail();
57+
}
58+
59+
/**
60+
* @return bool
61+
*/
62+
public function isFail(): bool
63+
{
64+
return count($this->_errors) > 0;
65+
}
66+
67+
/**
68+
* @return bool
69+
*/
70+
public function fail(): bool
71+
{
72+
return $this->isFail();
73+
}
74+
75+
/**
76+
* @return bool
77+
*/
78+
public function passed(): bool
79+
{
80+
return !$this->isFail();
81+
}
82+
83+
/**
84+
* @return bool
85+
*/
86+
public function isPassed(): bool
87+
{
88+
return !$this->isFail();
89+
}
90+
91+
/**
92+
* @param string $attr
93+
* @param string $msg
94+
*/
95+
public function addError(string $attr, string $msg)
96+
{
97+
$this->_errors[] = [$attr => $msg];
98+
}
99+
100+
/**
101+
* @return array
102+
*/
103+
public function getErrors(): array
104+
{
105+
return $this->_errors;
106+
}
107+
108+
/**
109+
* 得到第一个错误信息
110+
* @author inhere
111+
* @param bool $onlyMsg
112+
* @return array|string
113+
*/
114+
public function firstError($onlyMsg = true)
115+
{
116+
$e = $this->_errors;
117+
$first = array_shift($e);
118+
119+
return $onlyMsg ? array_values($first)[0] : $first;
120+
}
121+
122+
/**
123+
* 得到最后一个错误信息
124+
* @author inhere
125+
* @param bool $onlyMsg
126+
* @return array|string
127+
*/
128+
public function lastError($onlyMsg = true)
129+
{
130+
$e = $this->_errors;
131+
$last = array_pop($e);
132+
133+
return $onlyMsg ? array_values($last)[0] : $last;
134+
}
135+
136+
/**
137+
* @param bool|null $stopOnError
138+
* @return $this
139+
*/
140+
public function setStopOnError($stopOnError = null)
141+
{
142+
if (null !== $stopOnError) {
143+
$this->_stopOnError = (bool)$stopOnError;
144+
}
145+
146+
return $this;
147+
}
148+
149+
/**
150+
* @return bool
151+
*/
152+
public function isStopOnError(): bool
153+
{
154+
return $this->_stopOnError;
155+
}
156+
}

src/Utils/ErrorMessage.php

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

0 commit comments

Comments
 (0)