mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 00:32:16 +02:00
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:
parent
e70c201691
commit
83bb6e3fc9
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
16
tests/integration/issue-619.phpt
Normal file
16
tests/integration/issue-619.phpt
Normal 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
|
19
tests/integration/issue-805.phpt
Normal file
19
tests/integration/issue-805.phpt
Normal 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!!!!!!
|
||||
)
|
|
@ -15,4 +15,3 @@ try {
|
|||
?>
|
||||
--EXPECTF--
|
||||
- "something" is not tasty
|
||||
- "something" must be greater than or equal to 1
|
||||
|
|
Loading…
Reference in a new issue