Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add ability to throw a custom validation exception #38406

Merged
merged 5 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;
use Illuminate\Support\ValidatedInput;
use InvalidArgumentException;
use RuntimeException;
use stdClass;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -275,6 +276,13 @@ class Validator implements ValidatorContract
*/
protected $dotPlaceholder;

/**
* The exception to throw upon failure.
*
* @var string
*/
protected $exception = ValidationException::class;

/**
* Create a new Validator instance.
*
Expand Down Expand Up @@ -475,9 +483,7 @@ protected function removeAttribute($attribute)
*/
public function validate()
{
if ($this->fails()) {
throw new ValidationException($this);
}
throw_if($this->fails(), $this->exception, $this);

return $this->validated();
}
Expand Down Expand Up @@ -520,9 +526,7 @@ public function safe()
*/
public function validated()
{
if ($this->invalid()) {
throw new ValidationException($this);
}
throw_if($this->invalid(), $this->exception, $this);

$results = [];

Expand Down Expand Up @@ -1375,6 +1379,24 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
$this->presenceVerifier = $presenceVerifier;
}

/**
* Set the exception to throw upon failed validation.
*
* @param string $exception
* @return void
*
* @throws InvalidArgumentException
*/
public function setException($exception)
{
if (! is_a($exception, ValidationException::class, true)) {
throw new InvalidArgumentException(
sprintf('Exception [%s] is invalid. It must extend [%s].', $exception, ValidationException::class)
);
}
$this->exception = $exception;
}

/**
* Get the Translator implementation.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,34 @@ public function testCustomValidationLinesAreRespectedWithAsterisks()
$this->assertSame('english is required!', $v->messages()->first('lang.en'));
}

public function testCustomException()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['name' => ''], ['name' => 'required']);

$exception = new class($v) extends ValidationException {};
$v->setException($exception);

try {
$v->validate();
} catch (ValidationException $e) {
$this->assertSame($exception, $e);
}
}

public function testCustomExceptionMustExtendValidationException()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, [], []);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Exception [RuntimeException] is invalid. It must extend [Illuminate\Validation\ValidationException].');

$v->setException(\RuntimeException::class);
}

public function testValidationDotCustomDotAnythingCanBeTranslated()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down