Skip to content

Commit

Permalink
fix(V): 校验规则传入关联数组作为选项无效
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Oct 26, 2020
1 parent e1e5169 commit b62cd31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/V.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
* @method $this notUppercase()
*
* Length
* @method $this length($length, $min = null, $max = null) Check if the length (or size) is equals or in specified range
* @method $this length($length, $min = null, $max = null) Check if the length ( or size) is equals or in specified range
* @method $this notLength($length, $min = null, $max = null)
* @method $this charLength($length) Check if the characters length of input is equals specified length
* @method $this notCharLength($length)
* @method $this minLength($min) Check if the length (or size) of input is greater than specified length
* @method $this minLength($min) Check if the length ( or size) of input is greater than specified length
* @method $this notMinLength($min)
* @method $this maxLength($max) Check if the length (or size) of input is lower than specified length
* @method $this maxLength($max) Check if the length ( or size) of input is lower than specified length
* @method $this notMaxLength($max)
*
* Comparison
Expand Down Expand Up @@ -214,6 +214,12 @@ public function __call($name, $args)
if (method_exists($this, $name)) {
return $this->{$name}(...$args);
}

// Convert options [[option: xx]] to [option: xx]
if (count($args) === 1 && is_array($args[0])) {
$args = $args[0];
}

return $this->addRule($name, $args);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/unit/VTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ public function testCheckBeforeModelUpdate()
$this->assertRetSuc($ret);
}

public function testOptions()
{
$ret = V::key('name')->maxLength([
'max' => 1,
'countByChars' => false,
])->check([
'name' => '',
]);
$this->assertRetErr($ret, null, 'This value must have a length lower than 1');

$ret = V::key('name')->maxLength([
'max' => 1,
'countByChars' => true,
])->check([
'name' => '',
]);
$this->assertRetSuc($ret);
}

protected function checkModel(bool $isNew, $data)
{
return V::key('name', 'Name')->required($isNew)->notBlank()
Expand Down

0 comments on commit b62cd31

Please sign in to comment.