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

Conversation

a-h-abid
Copy link
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
18 checks passed
@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.

None yet

2 participants