Hello Geek, In today’s Laravel Validation Custom Error Messages example, we’ll take a look at a custom error message Laravel validator. If you have a question about using a custom validation message in a controller in Laravel, I’ll provide a simple example and solution. You will learn how to create custom error messages in Laravel form validation. For the Laravel change validation error message, you will do the following.
Its function returns three values: attribute, value, and fail. The attribute is the field for which validation is taking place. The value is the actual value of the object, and failure is the callback method that is executed if the validation fails.
This example works with Laravel 6, Laravel 7, Laravel 8, and Laravel 9 versions.
You simply need to follow the steps outlined below:
Step 1: Install Laravel
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command or learn how to install laravel in our other tutorial How to install laravel 9 – Laravel Installation Guide.
composer create-project laravel/laravel example-app
Example 1: Using Language File
We will set custom messages by directly on laravel set default files. but it will change in your whole project. So here bellow i added controller validation code for my user module like as bellow.
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$users = User::all();
return view('users.index', compact('users'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|same:confirm_password'
]);
$input = $request->all();
$input['password'] = bcrypt($input['password']);
User::create($input);
return redirect(route('users.index'));
}
}
Now I want to change the validation error message for the “name” field, so open the “validation.php” file and make the following changes:
resources/lang/en/validation.php
....
'custom' => [
'name' => [
'required' => 'The :attribute field can not be blank value.',
],
],
....
Example 2: Directly in Controller Code
We can change directly from the controller method; I believe this is the best option if you want to make it quick.
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$users = User::all();
return view('users.index', compact('users'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|same:confirm_password'
],
[ 'name.required' => 'The :attribute field can not be blank value.']);
$input = $request->all();
$input['password'] = bcrypt($input['password']);
User::create($input);
return redirect(route('users.index'));
}
}
Example 3: Using Custom Request
We must create a custom request and then use it in our controller; this is a better way of doing things in Laravel, so run the following command to create a user form request.
php artisan make:request UserFormRequest
app/Http/Requests/UserFormRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|same:confirm_password'
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'name.required' => 'The :attribute field can not be blank value',
];
}
}
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserFormRequest;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$users = User::all();
return view('users.index', compact('users'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(UserFormRequest $request)
{
$input = $request->all();
$input['password'] = bcrypt($input['password']);
User::create($input);
return redirect(route('users.index'));
}
}
All the best my fellow nerd.
Learn about more on how to Change Password with Current Password Validation in Laravel with Example