Skip to content

Commit 37a71de

Browse files
committed
Try to find template from default templates first
When executing `ValidationException::setTemplate()` using a template key, it does not try to select the template, but instead it uses the template key as the template itself. In order to fix this behaviour, there is now a check for a key with the defined template. In case the template was not found, use the defined template as the template itself.
1 parent d072b4d commit 37a71de

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

library/Exceptions/ValidationException.php

+3
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ public function hasCustomTemplate()
305305
public function setTemplate($template)
306306
{
307307
$this->customTemplate = true;
308+
if (isset(static::$defaultTemplates[$this->mode][$template])) {
309+
$template = static::$defaultTemplates[$this->mode][$template];
310+
}
308311
$this->template = $template;
309312

310313
$this->buildMessage();

tests/integration/issue-739.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\Validator as v;
8+
9+
try {
10+
v::when(v::alwaysInvalid(), v::alwaysValid())->check('foo');
11+
} catch (ValidationException $exception) {
12+
echo $exception->getMainMessage();
13+
}
14+
?>
15+
--EXPECT--
16+
"foo" is not valid

0 commit comments

Comments
 (0)