Skip to content

Commit d1426ff

Browse files
committed
Merge branch 'stevebauman/8.x' into 8.x
2 parents 8a861e7 + 07330ef commit d1426ff

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/Illuminate/Validation/Validator.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Support\MessageBag;
1616
use Illuminate\Support\Str;
1717
use Illuminate\Support\ValidatedInput;
18+
use InvalidArgumentException;
1819
use RuntimeException;
1920
use stdClass;
2021
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -275,6 +276,13 @@ class Validator implements ValidatorContract
275276
*/
276277
protected $dotPlaceholder;
277278

279+
/**
280+
* The exception to throw upon failure.
281+
*
282+
* @var string
283+
*/
284+
protected $exception = ValidationException::class;
285+
278286
/**
279287
* Create a new Validator instance.
280288
*
@@ -475,9 +483,7 @@ protected function removeAttribute($attribute)
475483
*/
476484
public function validate()
477485
{
478-
if ($this->fails()) {
479-
throw new ValidationException($this);
480-
}
486+
throw_if($this->fails(), $this->exception, $this);
481487

482488
return $this->validated();
483489
}
@@ -520,9 +526,7 @@ public function safe()
520526
*/
521527
public function validated()
522528
{
523-
if ($this->invalid()) {
524-
throw new ValidationException($this);
525-
}
529+
throw_if($this->invalid(), $this->exception, $this);
526530

527531
$results = [];
528532

@@ -1375,6 +1379,27 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
13751379
$this->presenceVerifier = $presenceVerifier;
13761380
}
13771381

1382+
/**
1383+
* Set the exception to throw upon failed validation.
1384+
*
1385+
* @param string $exception
1386+
* @return $this
1387+
*
1388+
* @throws \InvalidArgumentException
1389+
*/
1390+
public function setException($exception)
1391+
{
1392+
if (! is_a($exception, ValidationException::class, true)) {
1393+
throw new InvalidArgumentException(
1394+
sprintf('Exception [%s] is invalid. It must extend [%s].', $exception, ValidationException::class)
1395+
);
1396+
}
1397+
1398+
$this->exception = $exception;
1399+
1400+
return $this;
1401+
}
1402+
13781403
/**
13791404
* Get the Translator implementation.
13801405
*

tests/Validation/ValidationValidatorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,34 @@ 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+
$v->setException($exception);
638+
639+
try {
640+
$v->validate();
641+
} catch (ValidationException $e) {
642+
$this->assertSame($exception, $e);
643+
}
644+
}
645+
646+
public function testCustomExceptionMustExtendValidationException()
647+
{
648+
$trans = $this->getIlluminateArrayTranslator();
649+
650+
$v = new Validator($trans, [], []);
651+
652+
$this->expectException(\InvalidArgumentException::class);
653+
$this->expectExceptionMessage('Exception [RuntimeException] is invalid. It must extend [Illuminate\Validation\ValidationException].');
654+
655+
$v->setException(\RuntimeException::class);
656+
}
657+
630658
public function testValidationDotCustomDotAnythingCanBeTranslated()
631659
{
632660
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)