Skip to content

Commit

Permalink
[10.x] Use ValidationException class from Validator Property (#48736)
Browse files Browse the repository at this point in the history
* Use ValidationException class from Validator Property

* Remove unused import

* Update Validator.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
a-h-abid and taylorotwell authored Oct 16, 2023
1 parent 0d23fb3 commit 73ab11e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Validation\ValidatesWhenResolvedTrait;
use Illuminate\Validation\ValidationException;

class FormRequest extends Request implements ValidatesWhenResolved
{
Expand Down Expand Up @@ -152,7 +151,9 @@ public function validationData()
*/
protected function failedValidation(Validator $validator)
{
throw (new ValidationException($validator))
$exception = $validator->getException();

throw (new $exception($validator))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Validation/ValidatesWhenResolvedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ protected function passedValidation()
*/
protected function failedValidation(Validator $validator)
{
throw new ValidationException($validator);
$exception = $validator->getException();

throw new $exception($validator);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,16 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
$this->presenceVerifier = $presenceVerifier;
}

/**
* Get the exception to throw upon failed validation.
*
* @return string
*/
public function getException()
{
return $this->exception;
}

/**
* Set the exception to throw upon failed validation.
*
Expand Down
19 changes: 17 additions & 2 deletions tests/Validation/ValidationExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,26 @@ public function testExceptionGetResponseOneError()
$this->assertNull($exception->getResponse());
}

public function testGetExceptionClassFromValidator()
{
$validator = $this->getValidator();

$exception = $validator->getException();

$this->assertEquals(ValidationException::class, $exception);
}

protected function getException($data = [], $rules = [])
{
$translator = new Translator(new ArrayLoader, 'en');
$validator = new Validator($translator, $data, $rules);
$validator = $this->getValidator($data, $rules);

return new ValidationException($validator);
}

protected function getValidator($data = [], $rules = [])
{
$translator = new Translator(new ArrayLoader, 'en');

return new Validator($translator, $data, $rules);
}
}

0 comments on commit 73ab11e

Please sign in to comment.