Skip to content

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

Merged
taylorotwell merged 3 commits into
laravel:10.xfrom
a-h-abid:feat/validation-exception-from-validator-property
Oct 16, 2023
Merged

[10.x] Use ValidationException class from Validator Property#48736
taylorotwell merged 3 commits into
laravel:10.xfrom
a-h-abid:feat/validation-exception-from-validator-property

Conversation

@a-h-abid

Copy link
Copy Markdown
Contributor

Currently, the Validator class can take custom exception using setException method. So one can set custom ValiadtorException class as need and that will be used on validation error.

However in files FormRequest.php and ValidatesWhenResolvedTrait.php, the ValidationException class is hardcoded inside method failedValidation(). So if we use Form Requests, then it won't use the custom ValidationException class.

This PR makes it possible to use ValidationException class from the Validator class's $exception property. And it also makes possible to use custom exception fully throughout app.

How one can use custom ValidationException

By adding code like below in boot method of AppServiceProvider, you can use a custom validation exception.

\Illuminate\Support\Facades\Validator::resolver(function ($translator, $data, $rules, $messages, $customAttributes) {
    $validator = new \Illuminate\Validation\Validator($translator, $data, $rules, $messages, $customAttributes);
    $validator->setException(MyValidationException::class);
    return $validator;
});

@taylorotwell
taylorotwell merged commit 73ab11e into laravel:10.x Oct 16, 2023
@a-h-abid
a-h-abid deleted the feat/validation-exception-from-validator-property branch October 17, 2023 06:40
timacdonald pushed a commit to timacdonald/framework that referenced this pull request Oct 24, 2023
…#48736)

* Use ValidationException class from Validator Property

* Remove unused import

* Update Validator.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants