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

[10.x] Use ValidationException class from Validator Property #48736

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
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);
}
}