Skip to content

Commit

Permalink
Fix validating distinct for nested keys (#17102)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Jan 3, 2017
1 parent 16b3725 commit d15124e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,8 +1338,10 @@ protected function validateDistinct($attribute, $value, $parameters)

$attributeData = $this->extractDataFromPath($explicitPath);

$data = Arr::where(Arr::dot($attributeData), function ($value, $key) use ($attribute, $attributeName) {
return $key != $attribute && Str::is($attributeName, $key);
$pattern = str_replace('\*', '[^.]+', preg_quote($attributeName, '#'));

$data = Arr::where(Arr::dot($attributeData), function ($value, $key) use ($attribute, $attributeName, $pattern) {
return $key != $attribute && (bool) preg_match('#^'.$pattern.'\z#u', $key);
});

return ! in_array($value, array_values($data));
Expand Down
3 changes: 3 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,9 @@ public function testValidateDistinct()
$v = new Validator($trans, ['foo' => ['bar' => ['id' => 1], 'baz' => ['id' => 2]]], ['foo.*.id' => 'distinct']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => [['id' => 1, 'nested' => ['id' => 1]]]], ['foo.*.id' => 'distinct']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => [['id' => 1], ['id' => 1]]], ['foo.*.id' => 'distinct']);
$this->assertFalse($v->passes());

Expand Down

0 comments on commit d15124e

Please sign in to comment.