Fixed validation rule digits does not support int. (#2358)

* Fix strict_types=1, digits rule preg_match parameter 2 to be string error

* Fixed validation rule `digits` does not support `int`.

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
Kids Return 2020-08-27 11:09:13 +08:00 committed by GitHub
parent 87590fb644
commit 5a60792a50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View File

@ -10,6 +10,7 @@
- [#2331](https://github.com/hyperf/hyperf/pull/2331) Fixed exception thrown when the service or config was not found for nacos.
- [#2356](https://github.com/hyperf/hyperf/pull/2356) Fixed `server:start` failed, when the config of pid_file changed.
- [#2358](https://github.com/hyperf/hyperf/pull/2358) Fixed validation rule `digits` does not support `int`.
# v2.0.8 - 2020-08-24

View File

@ -301,8 +301,9 @@ trait ValidatesAttributes
{
$this->requireParameterCount(1, $parameters, 'digits');
$value = (string) $value;
return ! preg_match('/[^0-9]/', $value)
&& strlen((string) $value) == $parameters[0];
&& strlen($value) == $parameters[0];
}
/**
@ -314,7 +315,8 @@ trait ValidatesAttributes
{
$this->requireParameterCount(2, $parameters, 'digits_between');
$length = strlen((string) $value);
$value = (string) $value;
$length = strlen($value);
return ! preg_match('/[^0-9]/', $value)
&& $length >= $parameters[0] && $length <= $parameters[1];

View File

@ -1469,6 +1469,9 @@ class ValidationValidatorTest extends TestCase
$v = new Validator($trans, ['foo' => '123'], ['foo' => 'Digits:200']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['foo' => 123], ['foo' => 'Digits:200']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['foo' => '+2.37'], ['foo' => 'Digits:5']);
$this->assertTrue($v->fails());
@ -1482,6 +1485,9 @@ class ValidationValidatorTest extends TestCase
$v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'digits_between:1,10']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['foo' => 123], ['foo' => 'digits_between:4,5']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['foo' => '123'], ['foo' => 'digits_between:4,5']);
$this->assertFalse($v->passes());