Skip to content

Commit b24fb2d

Browse files
committed
Add ability to throw a custom validation exceptions
1 parent 4d1cdf4 commit b24fb2d

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/Illuminate/Validation/Validator.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Support\Fluent;
1515
use Illuminate\Support\MessageBag;
1616
use Illuminate\Support\Str;
17+
use InvalidArgumentException;
1718
use RuntimeException;
1819
use stdClass;
1920
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -265,6 +266,13 @@ class Validator implements ValidatorContract
265266
*/
266267
protected $dotPlaceholder;
267268

269+
/**
270+
* The exception to throw upon failure.
271+
*
272+
* @var string
273+
*/
274+
protected $exception = ValidationException::class;
275+
268276
/**
269277
* Create a new Validator instance.
270278
*
@@ -465,9 +473,7 @@ protected function removeAttribute($attribute)
465473
*/
466474
public function validate()
467475
{
468-
if ($this->fails()) {
469-
throw new ValidationException($this);
470-
}
476+
throw_if($this->fails(), $this->exception, $this);
471477

472478
return $this->validated();
473479
}
@@ -500,9 +506,7 @@ public function validateWithBag(string $errorBag)
500506
*/
501507
public function validated()
502508
{
503-
if ($this->invalid()) {
504-
throw new ValidationException($this);
505-
}
509+
throw_if($this->invalid(), $this->exception, $this);
506510

507511
$results = [];
508512

@@ -1349,6 +1353,25 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
13491353
$this->presenceVerifier = $presenceVerifier;
13501354
}
13511355

1356+
/**
1357+
* Set the exception to throw upon failed validation.
1358+
*
1359+
* @param string $exception
1360+
* @return void
1361+
*
1362+
* @throws InvalidArgumentException
1363+
*/
1364+
public function setException($exception)
1365+
{
1366+
if (! is_a($exception, ValidationException::class, true)) {
1367+
throw new InvalidArgumentException(
1368+
sprintf('Exception [%s] is invalid. It must extend [%s].', $exception, ValidationException::class)
1369+
);
1370+
}
1371+
1372+
$this->exception = $exception;
1373+
}
1374+
13521375
/**
13531376
* Get the Translator implementation.
13541377
*

tests/Validation/ValidationValidatorTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,35 @@ public function testCustomValidationLinesAreRespectedWithAsterisks()
627627
$this->assertSame('english is required!', $v->messages()->first('lang.en'));
628628
}
629629

630+
public function testCustomException()
631+
{
632+
$trans = $this->getIlluminateArrayTranslator();
633+
634+
$v = new Validator($trans, ['name' => ''], ['name' => 'required']);
635+
636+
$exception = new class ($v) extends ValidationException {};
637+
638+
$v->setException($exception);
639+
640+
try {
641+
$v->validate();
642+
} catch (ValidationException $e) {
643+
$this->assertSame($exception, $e);
644+
}
645+
}
646+
647+
public function testCustomExceptionMustExtendValidationException()
648+
{
649+
$trans = $this->getIlluminateArrayTranslator();
650+
651+
$v = new Validator($trans, [], []);
652+
653+
$this->expectException(\InvalidArgumentException::class);
654+
$this->expectExceptionMessage('Exception [RuntimeException] is invalid. It must extend [Illuminate\Validation\ValidationException].');
655+
656+
$v->setException(\RuntimeException::class);
657+
}
658+
630659
public function testValidationDotCustomDotAnythingCanBeTranslated()
631660
{
632661
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)