Skip to main content

Command Palette

Search for a command to run...

Laravel 12 Custom Validation Error Messages Example

How to Add Custom Error Messages in Laravel 12

Updated
3 min read
Laravel 12 Custom Validation Error Messages Example
I

Experienced Web Developer | Laravel | PHP | jQuery | HTML | CSS I’m a skilled web developer with extensive experience building dynamic and responsive web applications using Laravel, PHP, jQuery, HTML, and CSS. I specialize in creating clean, scalable, and maintainable code, with a focus on performance and user experience. Whether it's developing RESTful APIs, building custom CMS features, or crafting responsive front-ends, I bring a problem-solving mindset and a passion for quality to every project.

In this Laravel 12 guide, you’ll discover how to create and use custom validation error messages to make your forms more user-friendly and professional.

Laravel provides a powerful validation system with built-in rules like required, email, and min:8. By default, it shows generic error messages (for example, “The email field is required”). However, you can easily override these messages to make them more descriptive or match your app’s tone.

Laravel offers two main methods to define custom messages:

  1. Inline validation inside a controller

  2. Using a separate form request class

In this tutorial, we’ll start with the inline controller approach and create a simple user registration form containing name, email, password, confirm_password, phone, and avatar fields.

Method 1: Inline Custom Validation Messages in Controller

The quickest way to add custom messages is by defining them directly inside the validate() method in your controller.

Step 1: Generate a Controller

Run the artisan command below to create a new controller:

php artisan make:controller UserController

This will create a UserController file inside the app/Http/Controllers directory.

Step 2: Add Validation Logic with Custom Messages

Open your UserController.php file and update it with the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class UserController extends Controller
{    
     /**
     * Show the application form.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(): View
    {
        return view('form');
    }

     /**
     * Store data.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {
        $validatedData = $request->validate([
                'name' => 'required|string|max:55',
                'email' => 'required|email|unique:users,email',
                'password' => 'required|min:8|confirmed',
                'phone' => 'nullable|digits:10',
                'avtar' => 'required|mimes:jpeg,png,jpg|max:2048'
            ],[
                // Custom error messages
                'name.required' => 'Hey! We need your name to get started.',
                'name.max' => 'Your name is too long! Keep it under 55 characters.',
                'email.required' => 'Please provide your email address.',
                'email.email' => 'That doesn\'t look like a valid email address.',
                'email.unique' => 'This email is already registered. Try logging in instead!',
                'password.required' => 'You\'ll need a password to secure your account.',
                'password.min' => 'Your password should be at least 8 characters long.',
                'password.confirmed' => 'Password confirmation doesn\'t match.',
                'phone.digits' => 'Phone number must be exactly 10 digits.',
                'avtar.required' => 'Please upload your profile picture.',
                'avtar.mimes'    => 'Only JPEG, PNG, or JPG image formats are allowed.',
                'avtar.max'      => 'The image size must not exceed 2MB.',
            ]); 




        return back()->with('success', 'User created successfully.');
    }
}

Step 3: Define Routes

Next, add routes in your web.php file to display and handle the form:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('custom-validation', [UserController::class, 'create']);
Route::post('custom-validation', [UserController::class, 'store'])->name('validate');

Explanation

In the store() method:

  • The first array defines validation rules.

  • The second array lists the corresponding custom error messages.

Each key follows the format field.rule for example, 'email.email' or 'password.min'.

Laravel automatically redirects the user back with the relevant error messages if validation fails.

You can display these messages inside your Blade view using:

@error('field')
    <span class="text-danger">{{ $message }}</span>
@enderror

This approach keeps validation logic and custom messages close together, making your controller self-contained and easy to manage.

👉 Read full article here: Laravel 12 Custom Validation Error Messages Example