A quick method to have custom validation messages with Laravel

A quick method in Laravel to provide custom validation error messages.

This generally isn’t needed however if you want to provide more context to the validation fails you can utilize this.

There are a few ways to do this, here is the inline method where you included the custom messages along with the rules:

$validatedData = $request->validate([
    'key' => 'required|string|max:125|min:6',
], [
    'key.required' => 'The key is required!',
    'key.min' => 'Oops your key is too short, please ensure it has 6 or more characters',
    'key.max' => 'Oops your key is too long, please keep it at or below 125 characters',
    'key.string' => 'The key must be a string!'
]);

When ever the key field fails the 4 validation rules the custom error message for the respective rule will be sent.