diff --git a/library/Exceptions/NestedValidationException.php b/library/Exceptions/NestedValidationException.php index 4b207f49..2b3d741d 100644 --- a/library/Exceptions/NestedValidationException.php +++ b/library/Exceptions/NestedValidationException.php @@ -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(); } /** diff --git a/library/Rules/AbstractComposite.php b/library/Rules/AbstractComposite.php index daf577f0..b348ddd2 100644 --- a/library/Rules/AbstractComposite.php +++ b/library/Rules/AbstractComposite.php @@ -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); + } + } } diff --git a/tests/integration/issue-619.phpt b/tests/integration/issue-619.phpt new file mode 100644 index 00000000..da55f4ae --- /dev/null +++ b/tests/integration/issue-619.phpt @@ -0,0 +1,16 @@ +--FILE-- +setTemplate('invalid object')->assert('test'); +} catch (ValidationException $exception) { + print_r($exception->getMainMessage()); +} +?> +--EXPECTF-- +invalid object diff --git a/tests/integration/issue-805.phpt b/tests/integration/issue-805.phpt new file mode 100644 index 00000000..c1332cf6 --- /dev/null +++ b/tests/integration/issue-805.phpt @@ -0,0 +1,19 @@ +--FILE-- +setTemplate('WRONG EMAIL!!!!!!'))->assert(['email' => 'qwe']); +} catch (NestedValidationException $exception) { + print_r($exception->getMessages()); +} +?> +--EXPECTF-- +Array +( + [0] => WRONG EMAIL!!!!!! +) diff --git a/tests/integration/set_template_with_multiple_validators_should_use_template_as_full_message.phpt b/tests/integration/set_template_with_multiple_validators_should_use_template_as_full_message.phpt index 6af0ddc1..cfeb71f2 100644 --- a/tests/integration/set_template_with_multiple_validators_should_use_template_as_full_message.phpt +++ b/tests/integration/set_template_with_multiple_validators_should_use_template_as_full_message.phpt @@ -15,4 +15,3 @@ try { ?> --EXPECTF-- - "something" is not tasty - - "something" must be greater than or equal to 1