mirror of
https://github.com/Respect/Validation.git
synced 2024-05-06 00:13:16 +02:00
Do not hide messages on EachException
This commit is contained in:
parent
5fe4b96ebf
commit
bae314dd00
|
@ -9,6 +9,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
use function count;
|
||||
use function current;
|
||||
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
|
@ -27,4 +30,37 @@ final class EachException extends NestedValidationException
|
|||
self::STANDARD => 'Each item in {{name}} must not validate',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @todo This method shares too much with the parent implementation
|
||||
*/
|
||||
public function getMessages(array $templates = []): array
|
||||
{
|
||||
$messages = [$this->getId() => $this->renderMessage($this, $templates)];
|
||||
$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())
|
||||
);
|
||||
if (count($messages[$id . '.' . $count]) > 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$messages[$id . '.' . $count] = current($messages[$exception->getId()]);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -186,6 +186,42 @@ class NestedValidationException extends ValidationException implements IteratorA
|
|||
return implode(PHP_EOL, $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[]|string[][] $templates
|
||||
*/
|
||||
protected function renderMessage(ValidationException $exception, array $templates): string
|
||||
{
|
||||
if (isset($templates[$exception->getId()]) && is_string($templates[$exception->getId()])) {
|
||||
$exception->updateTemplate($templates[$exception->getId()]);
|
||||
}
|
||||
|
||||
return $exception->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[]|string[][] $templates
|
||||
* @param mixed ...$ids
|
||||
*
|
||||
* @return string[]|string[][]
|
||||
*/
|
||||
protected function findTemplates(array $templates, ...$ids): array
|
||||
{
|
||||
while (count($ids) > 0) {
|
||||
$id = array_shift($ids);
|
||||
if (!isset($templates[$id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_array($templates[$id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$templates = $templates[$id];
|
||||
}
|
||||
|
||||
return $templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RecursiveIteratorIterator<RecursiveExceptionIterator>
|
||||
*/
|
||||
|
@ -219,40 +255,4 @@ class NestedValidationException extends ValidationException implements IteratorA
|
|||
|
||||
return !$childException instanceof NonOmissibleException;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[]|string[][] $templates
|
||||
*/
|
||||
private function renderMessage(ValidationException $exception, array $templates): string
|
||||
{
|
||||
if (isset($templates[$exception->getId()]) && is_string($templates[$exception->getId()])) {
|
||||
$exception->updateTemplate($templates[$exception->getId()]);
|
||||
}
|
||||
|
||||
return $exception->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[]|string[][] $templates
|
||||
* @param mixed ...$ids
|
||||
*
|
||||
* @return string[]|string[][]
|
||||
*/
|
||||
private function findTemplates(array $templates, ...$ids): array
|
||||
{
|
||||
while (count($ids) > 0) {
|
||||
$id = array_shift($ids);
|
||||
if (!isset($templates[$id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_array($templates[$id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$templates = $templates[$id];
|
||||
}
|
||||
|
||||
return $templates;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use Respect\Validation\Validatable;
|
||||
use SplStack;
|
||||
|
@ -93,6 +94,27 @@ final class EachTest extends RuleTestCase
|
|||
$rule->check(range(1, 3));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function itShouldNotOverrideMessages(): void
|
||||
{
|
||||
$rule = new Each(new StringType());
|
||||
try {
|
||||
$rule->assert([1, 2, 3]);
|
||||
} catch (NestedValidationException $e) {
|
||||
$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',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Traversable<int>
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue