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

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

merged 5 commits into from
Aug 17, 2021

Conversation

stevebauman
Copy link
Contributor

@stevebauman stevebauman commented Aug 16, 2021

Description

This PR adds the ability to set a custom validation exception for manually created Validator instances via a setException() method.

The custom validation exception must extend the current ValidationException.

Purpose

This is super helpful for customizing the ValidationException with extra functionality that an application may need for specific validator instances.

The only other way of doing this right now (to my knowledge) is to extend the validator factory and modify the methods that throw the validation exceptions.

Example

class AppValidationException extends \Illuminate\Validation\ValidationException {}

// Somewhere in the application...

$validator = Validator::make($data, $rules, $messages);

$validator->setException(AppValidationException::class);

$validator->validate();

Notes

Let me know if you'd like anything adjusted or modified. No hard feelings on closure ❤️

Thanks for your time!

@driesvints
Copy link
Member

Seems like there's some conflicts here and tests aren't running yet. Please rebase this and make sure all builds pass. Then mark as ready to review. Thanks.

@driesvints driesvints marked this pull request as draft August 16, 2021 16:26
@stevebauman stevebauman marked this pull request as ready for review August 16, 2021 16:39
@stevebauman stevebauman changed the title [8.x] Add ability to throw a custom validation exceptions [8.x] Add ability to throw a custom validation exception Aug 16, 2021
@taylorotwell taylorotwell merged commit 73c7c11 into laravel:8.x Aug 17, 2021
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.

3 participants