diff --git a/library/Exceptions/EachException.php b/library/Exceptions/EachException.php index 480f7898..893ba09a 100644 --- a/library/Exceptions/EachException.php +++ b/library/Exceptions/EachException.php @@ -9,9 +9,6 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -use function count; -use function current; - /** * @author Alexandre Gomes Gaigalas * @author Henrique Moody @@ -38,27 +35,16 @@ final class EachException extends NestedValidationException */ public function getMessages(array $templates = []): array { - $messages = [$this->getId() => $this->renderMessage($this, $templates)]; + $messages = []; $count = -1; foreach ($this->getChildren() as $exception) { $count++; $id = $exception->getId(); - if (!$exception instanceof NestedValidationException) { - $messages[$id . '.' . $count] = $this->renderMessage( - $exception, - $this->findTemplates($templates, $this->getId()) - ); - continue; - } - $messages[$id . '.' . $count] = $exception->getMessages( - $this->findTemplates($templates, $id, $this->getId()) + $messages[$id . '.' . $count] = $this->renderMessage( + $exception, + $this->findTemplates($templates, $this->getId()) ); - if (count($messages[$id . '.' . $count]) > 1) { - continue; - } - - $messages[$id . '.' . $count] = current($messages[$exception->getId()]); } return $messages; diff --git a/tests/integration/issue-1348.phpt b/tests/integration/issue-1348.phpt new file mode 100644 index 00000000..62f41a44 --- /dev/null +++ b/tests/integration/issue-1348.phpt @@ -0,0 +1,46 @@ +--CREDITS-- +Alexandre Gomes Gaigalas +--FILE-- + 'Honda', 'model' => 'Accord'], + ['manufacturer' => 'Toyota', 'model' => 'Rav4'], + ['manufacturer' => 'Ford', 'model' => 'notarealcar'], + ['manufacturer' => 'Honda', 'model' => 'not valid'], +]; + +try { + Validator::arrayType()->each( + Validator::oneOf( + Validator::key('manufacturer', Validator::equals('Honda')) + ->key('model', Validator::in(['Accord', 'Fit'])), + Validator::key('manufacturer', Validator::equals('Toyota')) + ->key('model', Validator::in(['Rav4', 'Camry'])), + Validator::key('manufacturer', Validator::equals('Ford')) + ->key('model', Validator::in(['F150', 'Bronco'])) + ) + )->assert($cars); +} catch (NestedValidationException $e) { + var_dump($e->getMessages()); +} + + +?> +--EXPECT-- +array(1) { + ["each"]=> + array(2) { + ["validator.0"]=> + string(92) "All of the required rules must pass for `{ "manufacturer": "Ford", "model": "notarealcar" }`" + ["validator.1"]=> + string(91) "All of the required rules must pass for `{ "manufacturer": "Honda", "model": "not valid" }`" + } +} diff --git a/tests/unit/Rules/EachTest.php b/tests/unit/Rules/EachTest.php index 5e149b91..e94ea523 100644 --- a/tests/unit/Rules/EachTest.php +++ b/tests/unit/Rules/EachTest.php @@ -106,7 +106,6 @@ final class EachTest extends RuleTestCase $this->assertEquals( $e->getMessages(), [ - 'each' => 'Each item in `{ 1, 2, 3 }` must be valid', 'stringType.0' => '1 must be of type string', 'stringType.1' => '2 must be of type string', 'stringType.2' => '3 must be of type string',