Skip to content

Commit e1abd62

Browse files
committed
update
1 parent 7453c92 commit e1abd62

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

document.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ run: `composer update`
2020

2121
- Method 1: create a new class
2222
e.g.
23-
23+
2424
```php
2525

2626
use inhere\validate\Validation;
@@ -34,40 +34,39 @@ run: `composer update`
3434
['tagId', 'size', 'min'=>4, 'max'=>567], // 4<= tagId <=567
3535
['title', 'min', 'min' => 40],
3636
['freeTime', 'number'],
37-
['tagId', 'number', 'when' => function($data)
37+
['tagId', 'number', 'when' => function($data)
3838
{
3939
return isset($data['status']) && $data['status'] > 2;
4040
}],
41-
['userId', 'number', 'scene' => 'scene1' ],
42-
['userId', 'int', 'scene' => 'scene2' ],
41+
['userId', 'number', 'on' => 'scene1' ],
42+
['userId', 'int', 'on' => 'scene2' ],
4343
['title', 'customValidator', 'msg' => '{attr} error msg!' ],
4444
['status', function($status)
45-
{
45+
{
4646

4747
if ( .... ) {
4848
return true;
4949
}
5050
return false;
5151
}],
5252
];
53-
}
54-
53+
5554
// custom validator at the class. return a bool.
5655
protected function customValidator($title)
5756
{
5857
// some logic ...
59-
58+
6059
return true; // Or false;
6160
}
62-
61+
6362
// define field attribute's translate.
6463
public function attrTrans()
6564
{
6665
return [
6766
'userId' => '用户Id',
6867
];
6968
}
70-
69+
7170
// custom validator message, more {@see ValidationTrait::_defaultMessages}
7271
public function messages()
7372
{
@@ -87,7 +86,7 @@ if ( $valid->fail() ) {
8786
return $valid->getErrors();
8887
}
8988
...
90-
89+
9190
```
9291

9392

@@ -97,7 +96,7 @@ if ( $valid->fail() ) {
9796

9897
use inhere\validate\Validation;
9998

100-
class SomeClass
99+
class SomeClass
101100
{
102101
public function demo()
103102
{
@@ -111,7 +110,7 @@ if ( $valid->fail() ) {
111110
return $valid->getErrors();
112111
}
113112

114-
//
113+
//
115114
// some logic ... ...
116115
}
117116
}
@@ -132,14 +131,14 @@ $valid = Validation::make($_POST,[
132131
])
133132
->addValidator('checkTitle',function($title){
134133
// some logic ...
135-
134+
136135
return true; // if validate fail, return false.
137136
}, '{attr} default message!')
138137
->validate();
139138

140139
```
141140

142-
### keywords
141+
### keywords
143142

144143
- scene -- 设置验证场景
145144

@@ -148,15 +147,15 @@ $valid = Validation::make($_POST,[
148147
```php
149148

150149
// at a subclass of the Validation class
151-
<?php
152-
153-
public function rules()
150+
<?php
151+
152+
public function rules()
154153
{
155154
return [
156155
['title', 'required' ],
157-
['userId', 'number', 'scene' => 'scene1' ],
158-
['userId', 'int', 'scene' => 'scene2' ],
159-
['name', 'string', 'scene' => 'scene1,scene2' ],
156+
['userId', 'number', 'on' => 'scene1' ],
157+
['userId', 'int', 'on' => 'scene2' ],
158+
['name', 'string', 'on' => 'scene1,scene2' ],
160159
];
161160
}
162161
```
@@ -165,7 +164,7 @@ $valid = Validation::make($_POST,[
165164
166165
```php
167166

168-
// at logic
167+
// at logic
169168
<?php
170169

171170
...
@@ -184,12 +183,12 @@ $valid = Validation::make($_POST,[
184183
```php
185184

186185
// at a subclass of the Validation class
187-
<?php
188-
public function rules()
186+
<?php
187+
public function rules()
189188
{
190189
return [
191190
['title', 'required' ],
192-
['tagId', 'number', 'when' => function($data, $validator)
191+
['tagId', 'number', 'when' => function($data, $validator)
193192
{
194193
return isset($data['status']) && $data['status'] > 2;
195194
}],
@@ -232,7 +231,7 @@ $valid = Validation::make($_POST,[
232231
}]
233232
```
234233

235-
### Existing validators
234+
### Existing validators
236235

237236
validator | description | rule example
238237
----------|-------------|------------
@@ -252,4 +251,4 @@ validator | description | rule example
252251
`in` | validate in | `['id', 'in', 'value' => [1,2,3],]`
253252
`string` | validate string | ....
254253
`isArray` | validate is Array | ....
255-
`callback` | validate by custom callback | ....
254+
`callback` | validate by custom callback | ....

src/ValidationTrait.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ trait ValidationTrait
2626

2727
////////////////////////////////////////// validate data //////////////////////////////////////////
2828

29+
30+
/**
31+
* Through the validation of the data
32+
* @var array
33+
*/
34+
private $_safeData = [];
35+
2936
/**
3037
* 保存所有的验证错误信息
3138
* @var array[]
@@ -45,26 +52,27 @@ trait ValidationTrait
4552
private $_hasErrorStop = false;
4653

4754
/**
55+
* all rules
4856
* @var array
4957
*/
5058
private $_rules = [];
5159

5260
/**
61+
* available rules at current scene
5362
* @var array
5463
*/
55-
private $_validators = [];
64+
private $_availableRules = [];
5665

5766
/**
58-
* attribute field translate list
5967
* @var array
6068
*/
61-
private $_attrTrans = [];
69+
private $_validators = [];
6270

6371
/**
64-
* Through the validation of the data
72+
* attribute field translate list
6573
* @var array
6674
*/
67-
private $_safeData = [];
75+
private $_attrTrans = [];
6876

6977
/**
7078
* @var bool
@@ -88,7 +96,7 @@ public function rules()
8896
[ 'tagId,userId', 'required', 'msg' => '{attr} is required!'],
8997
9098
// set scene is add -- when `$this->scene == 'add'` enable this rule.
91-
[ 'tagId', 'size', 'min'=>4, 'max'=>567, 'scene' => 'add' ],
99+
[ 'tagId', 'size', 'min'=>4, 'max'=>567, 'on' => 'add' ],
92100
93101
// use callback and custom error message
94102
[ 'userId', function($value){ echo "$value ttg tg tt";}, 'msg' => '{attr} is required!'],
@@ -195,6 +203,11 @@ public function validate(array $onlyChecked = [], $hasErrorStop=false)
195203
( $validator !== 'required' && $skipOnEmpty && call_user_func($isEmpty, $data, $attr))
196204
) {
197205
continue;
206+
207+
// mark attribute is safe. not need validate. like. 'created_at'
208+
} elseif ( $validator === 'safe' ) {
209+
$this->_safeData[$attr] = $data[$attr];
210+
continue;
198211
}
199212

200213
list($result,$validator) = $this->doValidate($data, $attr, $validator, $copy);
@@ -332,20 +345,20 @@ protected function collectRules()
332345
throw new \InvalidArgumentException("validator setting error!");
333346
}
334347

335-
if ( empty($rule['scene']) ) {
348+
if ( empty($rule['on']) ) {
336349
$availableRules[] = $rule;
337350
} else {
338-
$ruleScene = $rule['scene'];
351+
$ruleScene = $rule['on'];
339352
$ruleScene = is_string($ruleScene) ? array_filter(explode(',', $ruleScene),'trim') : (array)$ruleScene;
340353

341354
if ( in_array($scene,$ruleScene) ) {
342-
unset($rule['scene']);
355+
unset($rule['on']);
343356
$availableRules[] = $rule;
344357
}
345358
}
346359
}
347360

348-
return $availableRules;
361+
return ($this->_availableRules = $availableRules);
349362
}
350363

351364
//////////////////////////////////// error info ////////////////////////////////////

0 commit comments

Comments
 (0)