ValidationExceptionBuilder is a fluent interface for building and throwing validation exceptions in Laravel
applications. It provides a convenient way to create custom validation exceptions with redirects, error messages, and
additional parameters.
Here's a quick example of how to use the ValidationExceptionBuilder:
use Midnite81\Core\Validation\ValidationExceptionBuilder;
ValidationExceptionBuilder::make('Invalid input')
->redirectTo('/form')
->flash('Please correct the errors and try again.')
->throwException();This will throw a ValidationException with the message "Invalid input", redirect the user to '/form', and flash a
message to the session.
You can create a ValidationExceptionBuilder instance in two ways:
-
Using the static
make()method:$builder = ValidationExceptionBuilder::make('Custom message');
-
Direct instantiation:
$builder = new ValidationExceptionBuilder('Custom message');
If no message is provided, a default message will be used: "There is an error in your form".
Create a new ValidationExceptionBuilder instance with an optional error message.
Alias for make(). Create a new ValidationExceptionBuilder instance with the specified error message.
Set a custom message for the exception.
Set the URL to redirect to after validation failure.
Set the redirect to go back to the previous URL.
Set a named route to redirect to after validation failure.
Set the URL fragment (hash) to append to the redirect URL.
Set query parameters to append to the redirect URL.
Set the error bag name for the validation exception.
Enable session flashing with an optional custom message and key.
Set the exception class to be thrown.
Set a callback for creating the exception.
Throw the configured exception.
Throw the configured exception if the given condition is true.
Throw the configured exception unless the given condition is true.
ValidationExceptionBuilder::make('The email is invalid')
->redirectBack()
->throwException();ValidationExceptionBuilder::make('Invalid input')
->redirectTo('/users')
->withQueryParameters(['sort' => 'name', 'order' => 'asc'])
->throwException();ValidationExceptionBuilder::make('Access denied')
->redirectRoute('dashboard', ['user' => $userId])
->throwException();ValidationExceptionBuilder::make('Form submission failed')
->redirectBack()
->flash('Please correct the errors and try again.', 'warning')
->throwException();$someCondition = true;
ValidationExceptionBuilder::make('Conditional error')
->redirectBack()
->throwExceptionIf($someCondition);class MyCustomException extends Exception {}
ValidationExceptionBuilder::make('Something went wrong')
->withException(MyCustomException::class)
->throwException();ValidationExceptionBuilder::make('Custom handling required')
->withExceptionCallback(function ($message, $url, $errorBag) {
// Custom logic here
return new MyCustomException($message);
})
->throwException();$builder = new ValidationExceptionBuilder();
$builder->withMessage('Chained message')
->redirectTo('/custom-page')
->fragment('section1')
->throwException();