Skip to content

Commit 48a2262

Browse files
committed
update readme. bug fixed for string len vaidate
1 parent ba0cedc commit 48a2262

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public function get(string $key, $default = null)
566566
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
567567
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
568568
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
569-
`specialChars` | 相当于使用 `htmlspecialchars()` 转义数据 | `['content', 'string', 'filter' => 'specialChars'],`
569+
`escape/specialChars` | 相当于使用 `htmlspecialchars()` 转义数据 | `['content', 'string', 'filter' => 'specialChars'],`
570570
`quotes` | 应用 `addslashes()` 转义数据 | `['content', 'string', 'filter' => 'quotes'],`
571571

572572
<a name="built-in-validators"></a>
@@ -652,7 +652,7 @@ $v = Validation::make($_POST, [
652652
// ...
653653
```
654654

655-
### 一些补充说明
655+
### (注意)一些补充说明
656656

657657
- **请将 `required*` 系列规则写在规则列表的最前面**
658658
- 关于布尔值验证
@@ -675,9 +675,10 @@ $v = Validation::make($_POST, [
675675
['goods.pear', 'max', 30], //goods 下的 pear 值最大不能超过 30
676676
```
677677

678-
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度
679678
- `required*` 系列规则参考自 laravel
680-
- `size/range` `length` 可以只定义 min 最小值。 但是当定义了max 值时,必须同时定义最小值
679+
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度
680+
- `size/range` `length` 可以只定义 `min` 最小值。 但是 **当定义了 `max` 值时,必须同时定义最小值**
681+
- 验证大小范围 是包含边界值的
681682

682683
## 代码示例
683684

src/Filter/FilterList.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static function encoded($var, $flags = 0)
185185
}
186186

187187
/**
188-
* 应用 addslashes() 转义数据
188+
* 应用 addslashes() 转义数据
189189
* @param string $var
190190
* @return string
191191
*/
@@ -195,7 +195,7 @@ public static function quotes($var)
195195
}
196196

197197
/**
198-
* HTML 转义字符 '"<>& 以及 ASCII 值小于 32 的字符。
198+
* like htmlspecialchars(), HTML 转义字符 '"<>& 以及 ASCII 值小于 32 的字符。
199199
* @param string $var
200200
* @param int $flags 标志
201201
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
@@ -214,6 +214,16 @@ public static function specialChars($var, $flags = 0)
214214
return filter_var($var, FILTER_SANITIZE_SPECIAL_CHARS, $settings);
215215
}
216216

217+
/**
218+
* @param $var
219+
* @param int $flags
220+
* @return string
221+
*/
222+
public static function escape($var, $flags = 0)
223+
{
224+
return self::specialChars($var, $flags);
225+
}
226+
217227
/**
218228
* HTML 转义字符 '"<>& 以及 ASCII 值小于 32 的字符。
219229
* @param string $var

src/ValidatorList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public static function size($val, $min = null, $max = null)
228228
{
229229
$options = [];
230230

231-
if (is_numeric($val)) {
231+
if (\is_int($val)) {
232232
$val = (int)$val;
233233
} elseif (\is_string($val)) {
234234
$val = Helper::strlen(trim($val));

tests/FieldValidationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testValidate()
4747

4848
$errors = $v->getErrors();
4949
$this->assertNotEmpty($errors);
50-
$this->assertCount(4, $errors);
50+
$this->assertCount(3, $errors);
5151

5252
// var_dump($errors);
5353
}

tests/RuleValidationTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Inhere\Validate\Validation;
34
use PHPUnit\Framework\TestCase;
45
use Inhere\Validate\RuleValidation;
56

@@ -15,7 +16,7 @@ class RuleValidationTest extends TestCase
1516
// 'freeTime' => '1456767657', // filed not exists
1617
'note' => '',
1718
'status' => 2,
18-
'name' => 'john',
19+
'name' => '1234a2',
1920
'existsField' => 'test',
2021
'passwd' => 'password',
2122
'repasswd' => 'repassword',
@@ -75,6 +76,25 @@ public function testValidateFailed()
7576
$this->assertEquals($v->getSafe('tagId'), null);
7677
}
7778

79+
public function testValidateString()
80+
{
81+
$val = '123482';
82+
$v = Validation::make([
83+
'user_name' => $val
84+
], [
85+
['user_name', 'string', 'min' => 6],
86+
// ['user_name', 'string', 'max' => 16],
87+
])->validate();
88+
89+
$this->assertTrue($v->passed());
90+
$this->assertFalse($v->failed());
91+
92+
$errors = $v->getErrors();
93+
$this->assertEmpty($errors);
94+
$this->assertCount(0, $errors);
95+
$this->assertEquals($v->getSafe('user_name'), $val);
96+
}
97+
7898
protected function someRules()
7999
{
80100
return [

0 commit comments

Comments
 (0)