Fix wrong behavior when using templates

When a template is set for a chain of rules, does not really matter
which messages the chain can have, the only message to be used should be
the one based on the defined template.

This commit set the same template of a parent rule to its children's
exception. Our first thought was to set the template to its children
however that would mean that if another rule would be added to the chain
we would have to set it as well. Doing that to the children's exception
make sure we only do that once.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Wojciech Frącz 2017-09-14 11:12:25 +02:00 committed by Henrique Moody
parent e70c201691
commit 83bb6e3fc9
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 79 additions and 9 deletions

View file

@ -124,9 +124,27 @@ class NestedValidationException extends ValidationException implements IteratorA
private function isSkippable(ValidationException $exception)
{
return $exception instanceof self
&& 1 === $exception->getRelated()->count()
&& false === $exception->hasCustomTemplate();
if (!$exception instanceof self) {
return false;
}
if (1 !== $exception->getRelated()->count()) {
return false;
}
if (!$exception->hasCustomTemplate()) {
return true;
}
return $this->hasChildTemplate($exception);
}
private function hasChildTemplate(self $exception)
{
$exception->getRelated()->rewind();
$childException = $exception->getRelated()->current();
return $childException->getMessage() === $exception->getMessage();
}
/**

View file

@ -11,6 +11,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;
@ -109,16 +110,33 @@ abstract class AbstractComposite extends AbstractRule
protected function validateRules($input)
{
$validators = $this->getRules();
$exceptions = [];
foreach ($validators as $v) {
foreach ($this->getRules() as $rule) {
try {
$v->assert($input);
} catch (ValidationException $e) {
$exceptions[] = $e;
$rule->assert($input);
} catch (ValidationException $exception) {
$exceptions[] = $exception;
$this->setExceptionTemplate($exception);
}
}
return $exceptions;
}
private function setExceptionTemplate(ValidationException $exception)
{
if (null === $this->template || $exception->hasCustomTemplate()) {
return;
}
$exception->setTemplate($this->template);
if (!$exception instanceof NestedValidationException) {
return;
}
foreach ($exception->getRelated() as $relatedException) {
$this->setExceptionTemplate($relatedException);
}
}
}

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Rules\Instance;
try {
(new Instance('stdClass'))->setTemplate('invalid object')->assert('test');
} catch (ValidationException $exception) {
print_r($exception->getMainMessage());
}
?>
--EXPECTF--
invalid object

View file

@ -0,0 +1,19 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::key('email', v::email()->setTemplate('WRONG EMAIL!!!!!!'))->assert(['email' => 'qwe']);
} catch (NestedValidationException $exception) {
print_r($exception->getMessages());
}
?>
--EXPECTF--
Array
(
[0] => WRONG EMAIL!!!!!!
)

View file

@ -15,4 +15,3 @@ try {
?>
--EXPECTF--
- "something" is not tasty
- "something" must be greater than or equal to 1