Skip to content

Commit 83bb6e3

Browse files
fraczhenriquemoody
andcommitted
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 <[email protected]>
1 parent e70c201 commit 83bb6e3

5 files changed

+79
-9
lines changed

library/Exceptions/NestedValidationException.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,27 @@ private function getRecursiveIterator()
124124

125125
private function isSkippable(ValidationException $exception)
126126
{
127-
return $exception instanceof self
128-
&& 1 === $exception->getRelated()->count()
129-
&& false === $exception->hasCustomTemplate();
127+
if (!$exception instanceof self) {
128+
return false;
129+
}
130+
131+
if (1 !== $exception->getRelated()->count()) {
132+
return false;
133+
}
134+
135+
if (!$exception->hasCustomTemplate()) {
136+
return true;
137+
}
138+
139+
return $this->hasChildTemplate($exception);
140+
}
141+
142+
private function hasChildTemplate(self $exception)
143+
{
144+
$exception->getRelated()->rewind();
145+
$childException = $exception->getRelated()->current();
146+
147+
return $childException->getMessage() === $exception->getMessage();
130148
}
131149

132150
/**

library/Rules/AbstractComposite.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Respect\Validation\Rules;
1313

14+
use Respect\Validation\Exceptions\NestedValidationException;
1415
use Respect\Validation\Exceptions\ValidationException;
1516
use Respect\Validation\Validatable;
1617
use Respect\Validation\Validator;
@@ -109,16 +110,33 @@ protected function appendRule(Validatable $validator)
109110

110111
protected function validateRules($input)
111112
{
112-
$validators = $this->getRules();
113113
$exceptions = [];
114-
foreach ($validators as $v) {
114+
foreach ($this->getRules() as $rule) {
115115
try {
116-
$v->assert($input);
117-
} catch (ValidationException $e) {
118-
$exceptions[] = $e;
116+
$rule->assert($input);
117+
} catch (ValidationException $exception) {
118+
$exceptions[] = $exception;
119+
$this->setExceptionTemplate($exception);
119120
}
120121
}
121122

122123
return $exceptions;
123124
}
125+
126+
private function setExceptionTemplate(ValidationException $exception)
127+
{
128+
if (null === $this->template || $exception->hasCustomTemplate()) {
129+
return;
130+
}
131+
132+
$exception->setTemplate($this->template);
133+
134+
if (!$exception instanceof NestedValidationException) {
135+
return;
136+
}
137+
138+
foreach ($exception->getRelated() as $relatedException) {
139+
$this->setExceptionTemplate($relatedException);
140+
}
141+
}
124142
}

tests/integration/issue-619.phpt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--FILE--
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Respect\Validation\Exceptions\ValidationException;
7+
use Respect\Validation\Rules\Instance;
8+
9+
try {
10+
(new Instance('stdClass'))->setTemplate('invalid object')->assert('test');
11+
} catch (ValidationException $exception) {
12+
print_r($exception->getMainMessage());
13+
}
14+
?>
15+
--EXPECTF--
16+
invalid object

tests/integration/issue-805.phpt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--FILE--
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Respect\Validation\Exceptions\NestedValidationException;
7+
use Respect\Validation\Validator as v;
8+
9+
try {
10+
v::key('email', v::email()->setTemplate('WRONG EMAIL!!!!!!'))->assert(['email' => 'qwe']);
11+
} catch (NestedValidationException $exception) {
12+
print_r($exception->getMessages());
13+
}
14+
?>
15+
--EXPECTF--
16+
Array
17+
(
18+
[0] => WRONG EMAIL!!!!!!
19+
)

tests/integration/set_template_with_multiple_validators_should_use_template_as_full_message.phpt

-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ try {
1515
?>
1616
--EXPECTF--
1717
- "something" is not tasty
18-
- "something" must be greater than or equal to 1

0 commit comments

Comments
 (0)