Skip to content

Commit

Permalink
Always return a valid exception
Browse files Browse the repository at this point in the history
If calling the method `getExceptionForPath()` when the exception was
nested but had no child it was returning null. This commit ensures that
it always return a valid exception.
  • Loading branch information
henriquemoody committed Sep 19, 2016
1 parent d0a98ae commit 9805047
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions library/Exceptions/NestedValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private function getExceptionForPath($path, ValidationException $exception)
foreach ($exception as $subException) {
return $subException;
}

return $exception;
}

/**
Expand Down
61 changes: 61 additions & 0 deletions tests/integration/issue-690.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--FILE--
<?php

require 'vendor/autoload.php';

use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;

$input = [
'contact1' => [
'daymark' => 'ffesfewf2232313123212',
'building' => [
'enable' => 'd',
'blank' => 'ffesfewf2232313123212',
'powerdown' => '5',
'powerup' => 'wdwdwf',
],
],
'contact2' => [
'name' => 'wd',
'daymark' => 'ffesfewf2232313123212',
'building' => [
'enable' => '1',
'blank' => '1',
'powerdown' => '1',
'powerup' => '1',
],
],
];

try {
v::create()
->each(
v::create()
->key('name', v::length(1, 50))
->key('daymark', v::length(1, 50))
->key(
'building',
v::create()
->key('enable', v::length(2, 50))
->key('time', v::length(2, 50))
->key('powerdown', v::length(2, 50))
->key('powerup', v::length(2, 50))
)
)
->assert($input);
} catch (NestedValidationException $exception) {
print_r(array_filter($exception->findMessages([
'each.name' => 'Center name',
'each.building.enable' => 'Center time',
'each.building.powerdown' => 'Center time',
])));
}
?>
--EXPECTF--
Array
(
[each_name] => Center name
[each_building_enable] => Center time
[each_building_powerdown] => Center time
)

0 comments on commit 9805047

Please sign in to comment.