Skip to content

Commit

Permalink
update readme. bug fixed for string len vaidate
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 30, 2017
1 parent ba0cedc commit 48a2262
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ public function get(string $key, $default = null)
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
`specialChars` | 相当于使用 `htmlspecialchars()` 转义数据 | `['content', 'string', 'filter' => 'specialChars'],`
`escape/specialChars` | 相当于使用 `htmlspecialchars()` 转义数据 | `['content', 'string', 'filter' => 'specialChars'],`
`quotes` | 应用 `addslashes()` 转义数据 | `['content', 'string', 'filter' => 'quotes'],`

<a name="built-in-validators"></a>
Expand Down Expand Up @@ -652,7 +652,7 @@ $v = Validation::make($_POST, [
// ...
```

### 一些补充说明
### (注意)一些补充说明

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

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

## 代码示例

Expand Down
14 changes: 12 additions & 2 deletions src/Filter/FilterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function encoded($var, $flags = 0)
}

/**
* 应用 addslashes() 转义数据
* 应用 addslashes() 转义数据
* @param string $var
* @return string
*/
Expand All @@ -195,7 +195,7 @@ public static function quotes($var)
}

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

/**
* @param $var
* @param int $flags
* @return string
*/
public static function escape($var, $flags = 0)
{
return self::specialChars($var, $flags);
}

/**
* HTML 转义字符 '"<>& 以及 ASCII 值小于 32 的字符。
* @param string $var
Expand Down
2 changes: 1 addition & 1 deletion src/ValidatorList.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static function size($val, $min = null, $max = null)
{
$options = [];

if (is_numeric($val)) {
if (\is_int($val)) {
$val = (int)$val;
} elseif (\is_string($val)) {
$val = Helper::strlen(trim($val));
Expand Down
2 changes: 1 addition & 1 deletion tests/FieldValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testValidate()

$errors = $v->getErrors();
$this->assertNotEmpty($errors);
$this->assertCount(4, $errors);
$this->assertCount(3, $errors);

// var_dump($errors);
}
Expand Down
22 changes: 21 additions & 1 deletion tests/RuleValidationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Inhere\Validate\Validation;
use PHPUnit\Framework\TestCase;
use Inhere\Validate\RuleValidation;

Expand All @@ -15,7 +16,7 @@ class RuleValidationTest extends TestCase
// 'freeTime' => '1456767657', // filed not exists
'note' => '',
'status' => 2,
'name' => 'john',
'name' => '1234a2',
'existsField' => 'test',
'passwd' => 'password',
'repasswd' => 'repassword',
Expand Down Expand Up @@ -75,6 +76,25 @@ public function testValidateFailed()
$this->assertEquals($v->getSafe('tagId'), null);
}

public function testValidateString()
{
$val = '123482';
$v = Validation::make([
'user_name' => $val
], [
['user_name', 'string', 'min' => 6],
// ['user_name', 'string', 'max' => 16],
])->validate();

$this->assertTrue($v->passed());
$this->assertFalse($v->failed());

$errors = $v->getErrors();
$this->assertEmpty($errors);
$this->assertCount(0, $errors);
$this->assertEquals($v->getSafe('user_name'), $val);
}

protected function someRules()
{
return [
Expand Down

0 comments on commit 48a2262

Please sign in to comment.