Snippet

Laravel Fail Validation Helper

app/helpers.php
if (! function_exists('fail_validation')) {    function fail_validation(string $key, string $message)    {        throw ValidationException::withMessages([$key => $message]);    }}

Context

Laravel's validation system takes care of all the dirty work for you, including redirecting the user back to the previous page with the correct status code and storing your errors on the session for easier display. This helper allows you to take advantage of all the heavy lifting once your initial validation has passed, allowing you to fail validation when a Stripe HTTP request fails, for example.

Enjoyed the article? Consider sharing it on Twitter so others can enjoy it too :)

Receive project updates, article drafts & thoughts on your inbox every saturday.

Subscribe