Skip to content

Commit a97b347

Browse files
committed
Merge remote-tracking branch 'origin/master' into php5
# Conflicts: # README.md # src/FilterList.php # src/ValidationTrait.php
2 parents 831625e + 9098608 commit a97b347

14 files changed

+1851
-857
lines changed

README.md

Lines changed: 551 additions & 21 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=5.5.0"
16+
"php": ">=7.0.0"
1717
},
1818
"autoload": {
1919
"psr-4": {

document.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,22 @@ $valid = Validation::make($_POST,[
236236

237237
validator | description | rule example
238238
----------|-------------|------------
239-
`int` | validate int | ....
239+
`int` | validate int | `['userId', 'int']`
240240
`number` | validate number | ....
241241
`bool` | validate bool | ....
242242
`float` | validate float | ....
243+
`string` | validate string. support length check (like `length`) | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`
243244
`regexp` | validate regexp | ....
244245
`url` | validate url | ....
245246
`email` | validate email | ....
246247
`ip` | validate ip | ....
247248
`required` | validate required | `['tagId,userId', 'required' ]`
248-
`length` | validate length | ....
249-
`size` | validate size | `['tagId', 'size', 'min'=>4, 'max'=>567]`
249+
`size` | validate size, can validate `int`, `string`, `array` | `['tagId', 'size', 'min'=>4, 'max'=>567]` `['name', 'size', 'max' => 16]`
250+
`range` | alias `size` | same `size`
251+
`length` | like `size`, but only validate `string`, `array` length | ....
250252
`min` | validate min | `['title', 'min', 'value' => 40],]`
251253
`max` | validate max | ....
252254
`in` | validate in | `['id', 'in', 'value' => [1,2,3],]`
253-
`string` | validate string | ....
255+
`compare` | compare attr | `['passwd', 'compare', 'repasswd']`
254256
`isArray` | validate is Array | ....
255-
`callback` | validate by custom callback | ....
257+
`callback` | validate by custom callback | ....

example/index.php

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

example/DataModel.php renamed to examples/DataModel.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
*/
88
class DataModel
99
{
10-
1110
use \inhere\validate\ValidationTrait;
1211

1312
protected $data = [];
1413

15-
1614
/**
1715
* @param array $data
1816
* @return $this
@@ -23,6 +21,4 @@ public function setData($data)
2321

2422
return $this;
2523
}
26-
27-
28-
}
24+
}
File renamed without changes.

examples/index.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
spl_autoload_register(function($class)
4+
{
5+
// e.g. "inhere\validate\ValidationTrait"
6+
if (strpos($class,'\\')) {
7+
$file = dirname(__DIR__) . '/src/' . trim(strrchr($class,'\\'),'\\'). '.php';
8+
} else {
9+
$file = __DIR__ . '/' . $class. '.php';
10+
}
11+
12+
if (is_file($file)) {
13+
include $file;
14+
}
15+
});
16+
17+
$data = [
18+
// 'userId' => 234,
19+
'userId' => 'is not an integer',
20+
'tagId' => '234535',
21+
// 'freeTime' => '1456767657', // filed not exists
22+
'note' => '',
23+
'name' => 'Ajohn',
24+
'existsField' => 'test',
25+
'passwd' => 'password',
26+
'repasswd' => 'repassword',
27+
'insertTime' => '1456767657',
28+
'goods' => [
29+
'apple' => 34,
30+
'pear' => 50,
31+
],
32+
];
33+
34+
$rules = [
35+
['tagId,userId,freeTime', 'required', 'msg' => '{attr} is required!'],// set message
36+
['tagId,userId,freeTime', 'number'],
37+
['note', 'email', 'skipOnEmpty' => false], // set skipOnEmpty is false.
38+
['insertTime', 'email', 'scene' => 'otherScene' ],// set scene. will is not validate it on default.
39+
['tagId', 'size', 'max'=> 567, 'min'=> 4, ], // 4<= tagId <=567
40+
['passwd', 'compare', 'repasswd'], //
41+
42+
['name', 'regexp' ,'/^[a-z]\w{2,12}$/'],
43+
44+
['goods.pear', 'max', 30], //
45+
46+
['goods', 'isList'], //
47+
48+
['notExistsField1', 'requiredWithout', 'notExistsField2'], //
49+
// ['notExistsField1', 'requiredWithout', 'existsField'], //
50+
51+
['freeTime', 'size', 'min'=>4, 'max'=>567, 'when' => function($data, $valid) {
52+
echo " use when pre-check\n";
53+
54+
// $valid is current validation instance.
55+
56+
return true;
57+
}], // 4<= tagId <=567
58+
59+
['userId', function($value, $data){
60+
echo " use custom validate to check userId \n";
61+
62+
// var_dump($value, $data);
63+
// echo __LINE__ . "\n";
64+
65+
return false;
66+
}, 'msg' => 'userId check failure by closure!'],
67+
];
68+
69+
echo "\n----------------------------\n raw data, waiting to validate\n----------------------------\n\n";
70+
71+
print_r($data);
72+
73+
echo "\n----------------------------\n use ValidationTrait\n----------------------------\n\n";
74+
75+
//$model = new DataModel($_POST,$rules);
76+
$model = new DataModel;
77+
$model->setData($data)->setRules($rules);
78+
$model->validate();
79+
80+
print_r($model->getErrors());
81+
82+
echo "\n----------------------------\n use Validation\n----------------------------\n\n";
83+
84+
$valid = \inhere\validate\Validation::make($data, $rules)
85+
->setAttrTrans([
86+
'goods.pear' => '梨子'
87+
])
88+
->validate([], false);
89+
90+
print_r($valid->getErrors());
91+

0 commit comments

Comments
 (0)