Skip to content

Commit 06cfacd

Browse files
author
xiajianjun
committed
init project
1 parent 05e161d commit 06cfacd

File tree

11 files changed

+2170
-0
lines changed

11 files changed

+2170
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
#php-validate
2+
3+
> a simple validate library of the php
4+
5+
### Install
6+
7+
- use composer
8+
9+
edit `composer.json`
10+
11+
_require_ add
12+
13+
```
14+
"inhere/php-validate": "dev-master",
15+
```
16+
17+
_repositories_ add
18+
19+
```
20+
"repositories": [
21+
{
22+
"type": "git",
23+
"url": "https://git.oschina.net/inhere/php-validate"
24+
}
25+
]
26+
```
27+
28+
run: `composer update`
29+
30+
### Document
31+
32+
See [document](document.md)

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "inhere/php-validate",
3+
"type": "library",
4+
"description": "A Slim Framework extension",
5+
"keywords": ["Slim","extensions"],
6+
"homepage": "http://github.com/inhere/php-validate",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "inhere",
11+
"email": "[email protected]",
12+
"homepage": "http://www.yzone.net/"
13+
}
14+
],
15+
"require": {
16+
"php": ">=5.5.0"
17+
},
18+
"autoload":{
19+
"psr-4": {
20+
"inhere\\validate\\" : "src/"
21+
}
22+
},
23+
"suggest": {
24+
"inhere/simple-print-tool": "Very lightweight data printing tools"
25+
}
26+
}

document.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
## simple validator
2+
3+
### Install
4+
5+
- use composer
6+
7+
edit `composer.json`
8+
9+
_require_ add
10+
11+
```
12+
"inhere/php-validate": "dev-master",
13+
```
14+
15+
_repositories_ add
16+
17+
```
18+
"repositories": [
19+
{
20+
"type": "git",
21+
"url": "https://git.oschina.net/inhere/php-validate"
22+
}
23+
]
24+
```
25+
26+
run: `composer update`
27+
28+
### how to use
29+
30+
- Method 1: create a new class
31+
e.g.
32+
33+
```
34+
<?php
35+
36+
use slimExtend\validate\Validator;
37+
38+
class PageRequest extends Validator
39+
{
40+
public function rules()
41+
{
42+
return [
43+
['tagId,userId,freeTime', 'required', 'msg' => '{attr} is required!'],
44+
['tagId', 'size', 'min'=>4, 'max'=>567], // 4<= tagId <=567
45+
['title', 'min', 'min' => 40],
46+
['freeTime', 'number'],
47+
['tagId', 'number', 'when' => function($data)
48+
{
49+
return isset($data['status']) && $data['status'] > 2;
50+
}],
51+
['userId', 'number', 'scene' => 'scene1' ],
52+
['userId', 'int', 'scene' => 'scene2' ],
53+
['status', function($status)
54+
{
55+
56+
if ( .... ) {
57+
return true;
58+
}
59+
return false;
60+
}],
61+
];
62+
}
63+
64+
// define field attribute's translate.
65+
public function attrTrans()
66+
{
67+
return [
68+
'userId' => '用户Id',
69+
];
70+
}
71+
72+
// custom validator message
73+
public function messages()
74+
{
75+
return [
76+
'required' => '{attr} 是必填项。',
77+
];
78+
}
79+
}
80+
81+
//
82+
// use, at other class
83+
84+
$valid = Validator::make($_POST,)->validate();
85+
if ( $valid->fail() ) {
86+
return $valid->getErrors();
87+
}
88+
...
89+
90+
```
91+
92+
93+
- Method 2: direct use
94+
95+
```
96+
<?php
97+
use slimExtend\validate\Validator;
98+
99+
class SomeClass
100+
{
101+
public function demo()
102+
{
103+
$valid = Validator::make($_POST,[
104+
// add rule
105+
['title', 'min', 'min' => 40],
106+
['freeTime', 'number'],
107+
])->validate();
108+
109+
if ( $valid->fail() ) {
110+
return $valid->getErrors();
111+
}
112+
113+
//
114+
// some logic ... ...
115+
}
116+
}
117+
```
118+
119+
### keywords
120+
121+
- scene -- 设置验证场景
122+
> 如果需要让一个验证器在多个类似情形下使用,在验证时也表明要验证的场景
123+
124+
```
125+
// at validator class
126+
<?php
127+
128+
'''
129+
public function rules()
130+
{
131+
return [
132+
['title', 'required' ],
133+
['userId', 'number', 'scene' => 'scene1' ],
134+
['userId', 'int', 'scene' => 'scene2' ],
135+
];
136+
}
137+
```
138+
> 在下面设置了场景时,将只会使用上面的第 1 3 条规则. (第 1 条没有限制规则使用场景的,在所有场景都可用)
139+
140+
```
141+
// at logic
142+
<?php
143+
144+
...
145+
$valid = ValidatorClass::make($_POST)->setScene('scene2')->validate();
146+
...
147+
148+
```
149+
150+
- when -- 规则的前置条件
151+
> 只有在先满足了(`when`)前置条件时才会验证这条规则
152+
如在下面的例子中,检查到第二条规则时,会先执行闭包(`when`),
153+
当其返回 `true` 验证此条规则,
154+
否则不会验证此条规则
155+
156+
```
157+
// at validator class
158+
<?php
159+
160+
'''
161+
public function rules()
162+
{
163+
return [
164+
['title', 'required' ],
165+
['tagId', 'number', 'when' => function($data, $validator)
166+
{
167+
return isset($data['status']) && $data['status'] > 2;
168+
}],
169+
];
170+
}
171+
```
172+
173+
174+
### Existing validators
175+
176+
validator | description | rule example
177+
----------|-------------|------------
178+
`int` | validate int | ....
179+
`number` | validate number | ....
180+
`bool` | validate bool | ....
181+
`float` | validate float | ....
182+
`regexp` | validate regexp | ....
183+
`url` | validate url | ....
184+
`email` | validate email | ....
185+
`ip` | validate ip | ....
186+
`required` | validate required | ....
187+
`length` | validate length | ....
188+
`minLength` | validate minLength | ....
189+
`maxLength` | validate maxLength | ....
190+
`size` | validate size | ....
191+
`min` | validate min | ....
192+
`max` | validate max | ....
193+
`in` | validate in | ....
194+
`string` | validate string | ....
195+
`array` | validate is Array | ....
196+
`callback` | validate by custom callback | ....

example/DataModel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
class DataModel extends \slimExtend\validate\Validator
4+
{
5+
6+
7+
}

example/PageRequest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2016/2/19 0019
6+
* Time: 23:35
7+
*/
8+
9+
/**
10+
* Class PageRequest
11+
*/
12+
class PageRequest extends \slimExtend\validate\Validator
13+
{
14+
public function rules()
15+
{
16+
return [
17+
['tagId,userId,freeTime', 'required' ],
18+
['tagId', 'size', 'min'=>4, 'max'=>567], // 4<= tagId <=567
19+
['title', 'min', 'min' => 40],
20+
['freeTime', 'number', 'msg' => '{attr} is require number!'],
21+
['test', 'number', 'when' => function($data) {
22+
return isset($data['status']) && $data['status'] > 2;
23+
}],
24+
['userId', 'number', 'scene' => 'other' ],
25+
// ['userId', function($value){ return false;}],
26+
];
27+
}
28+
29+
public function attrTrans()
30+
{
31+
return [
32+
'userId' => '用户Id',
33+
];
34+
}
35+
36+
// custom validator message
37+
public function messages()
38+
{
39+
return [
40+
'required' => '{attr} 是必填项。',
41+
];
42+
}
43+
}

example/index.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
spl_autoload_register(function($class)
4+
{
5+
$file = __DIR__ . '/' . $class. '.php';
6+
7+
if (is_file($file)) {
8+
include $file;
9+
}
10+
});
11+
12+
$data = [
13+
'userId' => 'sdfdffffffffff',
14+
'tagId' => '234535',
15+
// 'freeTime' => 'sdfdffffffffff',
16+
'distanceRequire' => 'sdfdffffffffff',
17+
'note' => 'sdfdffffffffff',
18+
'insertTime' => '',
19+
'lastReadTime' => 'sdfdffffffffff',
20+
];
21+
22+
$rules = [
23+
['tagId,userId,freeTime', 'required', 'msg' => '{field} is required!'],
24+
['note', 'email'],
25+
['tagId', 'size', 'min'=>4, 'max'=>567, 'msg' => '{field} must is big!'], // 4<= tagId <=567
26+
['freeTime', 'size', 'min'=>4, 'max'=>567, 'msg' => '{field} must is big!'], // 4<= tagId <=567
27+
['userId', function($value){ echo $value."\n"; return false;}, 'msg' => '{field} check filare!'],
28+
];
29+
30+
/*
31+
$model = new TestModel();
32+
$ret = $model->load($_POST)->validate();
33+
*/
34+
$model = new DataModel($_POST,$rules);
35+
$ret = $model->validate([], true);
36+
37+
// echo "<pre>";
38+
var_dump($ret,
39+
$model->firstError()
40+
);
41+
42+
43+
// echo "</pre>";

0 commit comments

Comments
 (0)