Skip to content

Commit

Permalink
Try to find template from default templates first
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
henriquemoody committed Jan 2, 2018
1 parent d072b4d commit 37a71de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ public function hasCustomTemplate()
public function setTemplate($template)
{
$this->customTemplate = true;
if (isset(static::$defaultTemplates[$this->mode][$template])) {
$template = static::$defaultTemplates[$this->mode][$template];
}
$this->template = $template;

$this->buildMessage();
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/issue-739.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\Validator as v;

try {
v::when(v::alwaysInvalid(), v::alwaysValid())->check('foo');
} catch (ValidationException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECT--
"foo" is not valid

0 comments on commit 37a71de

Please sign in to comment.