Skip to content

Commit

Permalink
fix(IsEach): 每次校验时都要创建一个新的 V 对象,以便对每个数组项目自定义规则
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Mar 4, 2021
1 parent 25ab8b4 commit 6a47768
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/IsEach.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ protected function doValidate($input)
}

$result = true;
$options = $this->getValidatorOptions();

foreach ($input as $i => $row) {
$options = $this->getValidatorOptions();
$options['data'] = $row;
$validator = wei()->validate($options);
$this->selfValidators[$i] = $validator;
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/IsEachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ public function testCallableParameter()
$this->assertRetErr($ret, 'The 2nd 用户\'s 姓名 must have a length greater than 3');
}

public function testCreateNewInstanceEveryTime()
{
$validators = [];
$ret = V
::key('users', '用户')->each(function (V $v) use (&$validators) {
$validators[] = $v;
$v->string('name', '姓名')->minLength(3)
->string('email', '邮箱')->email();
})
->check([
'users' => [
[
'name' => 'test',
'email' => 'test@example.com',
],
[
'name' => 't',
'email' => 't',
],
],
]);
$this->assertRetErr($ret, 'The 2nd 用户\'s 姓名 must have a length greater than 3');
$this->assertNotSame($validators[0], $validators[1]);
}

public function testNotArray()
{
$ret = V
Expand Down

0 comments on commit 6a47768

Please sign in to comment.