Skip to content

Commit

Permalink
Fix wrong behavior when using templates
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
fracz and henriquemoody committed Jul 2, 2018
1 parent e70c201 commit 83bb6e3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
24 changes: 21 additions & 3 deletions library/Exceptions/NestedValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,27 @@ private function getRecursiveIterator()

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();
}

/**
Expand Down
28 changes: 23 additions & 5 deletions library/Rules/AbstractComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,16 +110,33 @@ protected function appendRule(Validatable $validator)

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);
}
}
}
16 changes: 16 additions & 0 deletions tests/integration/issue-619.phpt
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions tests/integration/issue-805.phpt
Original file line number Diff line number Diff line change
@@ -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!!!!!!
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ try {
?>
--EXPECTF--
- "something" is not tasty
- "something" must be greater than or equal to 1

0 comments on commit 83bb6e3

Please sign in to comment.