Laravel 10 Custom Validation Error Message

Websolutionstuff | May-19-2023 | Categories : Laravel

In this article, we will see the laravel 10 custom validation error message. Here, we will learn about how to create custom validation rules in laravel 10. Laravel provides several different approaches to validate your application's incoming data.

As you can see, the validation rules are passed into the validate method. Don't worry - all available validation rules are documented. Again, if the validation fails, the proper response will automatically be generated. Also, we will use validate() method for adding validation rules and custom messages. 

So, let's see how to create a custom validation rule in laravel 10, custom laravel validation message, how to customize a validation error message in laravel 10, and how to create a custom message in laravel 10.

You have to add a seconds argument to validate the () method with a custom error message array.

$request->validate([
    'name' => 'required',
    'email' => 'required|email|unique:users'
    'password' => 'required|min:5',    
], [
    'name.required' => 'Name field is required.',
    'email.required' => 'Email field is required.',
    'email.email' => 'Email field must be email address.'
    'password.required' => 'Password field is required.',
]);

 

Step 1: Install Laravel 10

In this step, we will install laravel 10 for custom error messages. So, run the following command.

composer create-project laravel/laravel example-app

 

Step 2: Create Controller

Then, we will create UserController using the following command. And in this controller, we will have two methods create and store.

php artisan make:controller UserController

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
  
class UserController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(): View
    {
        return view('index');
    }
      
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {
  
        $validatedData = $request->validate([
                'name' => 'required',
                'email' => 'required|email|unique:users'
                'password' => 'required|min:5',
            ], [
                'name.required' => 'Name field is required.',
                'email.required' => 'Email field is required.',
                'email.email' => 'Email field must be email address.'
                'password.required' => 'Password field is required.',
            ]);
    
        $validatedData['password'] = bcrypt($validatedData['password']);
        $user = User::create($validatedData);
          
        return back()->with('success', 'User created successfully.');
    }
}

 

Step 3: Create Routes

Now, add the routes to the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
  
/* 
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('user/create', [ UserController::class, 'create' ]);
Route::post('user/create', [ UserController::class, 'store' ]);

 

Step 4: Create Blade File

Then, we will create an index.blade.php file. So, add the following HTML code to that file.

resources/views/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 10 Custom Validation Error Message - Websolutionstuff</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
    
        <h1>Laravel 10 Custom Validation Error Message - Websolutionstuff</h1>
       
        @if(Session::has('success'))
        <div class="alert alert-success">
            {{ Session::get('success') }}
            @php
                Session::forget('success');
            @endphp
        </div>
        @endif
    
        <!-- Way 1: Display All Error Messages -->
        @if ($errors->any())
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.<br><br>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
       
        <form method="POST" action="{{ url('user/create') }}">
      
            {{ csrf_field() }}
      
            <div class="mb-3">
                <label class="form-label" for="inputName">Name:</label>
                <input type="text" name="name" id="inputName" class="form-control @error('name') is-invalid @enderror" placeholder="Name">
  
                <!-- Way 2: Display Error Message -->
                @error('name')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="mb-3">
                <label class="form-label" for="inputEmail">Email:</label>
                <input type="text" name="email" id="inputEmail" class="form-control @error('email') is-invalid @enderror" placeholder="Email">
  
                @error('email')
                    <span class="text-danger">{{ $message }}</span>
                @endif
            </div>
     
            <div class="mb-3">
                <label class="form-label" for="inputPassword">Password:</label>
                <input type="password" name="password" id="inputPassword" class="form-control @error('password') is-invalid @enderror" placeholder="Password">
  
                <!-- Way 3: Display Error Message -->
                @if ($errors->has('password'))
                    <span class="text-danger">{{ $errors->first('password') }}</span>
                @endif
            </div>
     
            <div class="mb-3">
                <button class="btn btn-success btn-submit">Submit</button>
            </div>
        </form>
    </div>
</body>
</html>

 


You might also like:

Recommended Post
Featured Post
How To Use Google Recaptcha V2 In Laravel 9
How To Use Google Recaptcha V2...

In this article, we will see how to use google ReCaptcha v2 in laravel 9. Google ReCaptcha is used for advance...

Read More

Jun-14-2022

Paginate Method Example in Laravel 8
Paginate Method Example in Lar...

In this post i will share you information about paginate method example in laravel 8. As we know laravel provide many...

Read More

Jun-28-2021

Laravel 11 Livewire 3 wire:submit - Form Submit Event
Laravel 11 Livewire 3 wire:sub...

In this tutorial, I will guide you on how to handle form submission in Laravel 11 using Livewire 3. With Livewire 3, you...

Read More

Mar-20-2025

PHP Array Functions With Example
PHP Array Functions With Examp...

In this tutorial we will learn php array functions with example. An array is a data structure that contains a group of e...

Read More

Apr-23-2021