Laravel Validation with Custom Error Messages
Hello guys, today I’m going to show you how to display validation error custom messages. I’ll share three methods to change the error messages. Let’s start:
Table of Contents
Step 1 : Set Error Messages in Controller
We can easily change custom error messages in the controller. This is a quick solution. Have a look at this method:
<?php
namespace App\Http\Controllers;
use App\Book;
use Illuminate\Http\Request;
use Validator;
class BookController extends Controller
{
public function store(Request $request)
{
// validator
$validator = Validator::make($request->all(), [
'name' => 'required',
'author' => 'required',
], [
'name.required' => 'Please enter book name',
'author.required' => 'Please enter book author',
]);
// check validation
if ($validator->fails()) {
$response = [
'success' => false,
'message' => $validator->messages()
];
return response()->json($response, 404);
}
// try to store the book
try {
$input = $request->all();
Book::create($input);
$success = true;
$message = "Stored successful";
} catch (\Illuminate\Database\QueryException $ex) {
$success = false;
$data = null;
$message = $ex->getMessage();
}
// make response
$response = [
'success' => $success,
'message' => $message
];
// return response
return response()->json($response, 200);
}
}
Step 2 : Adding Error Messages in Language File
This is the last method. If we set custom error messages here, these will work in the whole project.
Open resources/lang/en/validation.php
file and add custom errors with messages in the “custom” array. Here’s an example:
'custom' => [
'name' => [
'required' => 'Please enter book name',
],
'author' => [
'required' => 'Please enter book author',
],
],
and the BookController looks like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use Validator;
class BookController extends Controller
{
public function store(Request $request)
{
// validator
$validator = Validator::make($request->all(), [
'name' => 'required',
'author' => 'required',
]);
// check validation
if ($validator->fails()) {
$response = [
'success' => false,
'message' => $validator->messages()
];
return response()->json($response, 404);
}
// try to store the book
try {
$input = $request->all();
Book::create($input);
$success = true;
$message = "Book successfully stored";
} catch (\Illuminate\Database\QueryException $ex) {
$success = false;
$message = $ex->getMessage();
}
// make response
$response = [
'success' => $success,
'message' => $message
];
// return response
return response()->json($response, 200);
}
}
Step 3 : Creating Custom Request
By creating a custom request we can do this. To do this, we have to create a request. Let’s create a request by using this artisan command:
php artisan make:request BookFormRequest
Open the BookFormRequest from app\Http\Requests
and paste this code:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BookFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'author' => 'required',
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'name.required' => 'Please enter book name',
'author.required' => 'Please enter book author',
];
}
}
Let’s use this BookFormRequest request in our BookController.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\BookFormRequest;
use App\Book;
class BookController extends Controller
{
public function store(BookFormRequest $request)
{
// try to store the book
try {
$input = $request->all();
Book::create($input);
$success = true;
$message = "Book successfully stored";
} catch (\Illuminate\Database\QueryException $ex) {
$success = false;
$message = $ex->getMessage();
}
// make response
$response = [
'success' => $success,
'message' => $message
];
// return response
return response()->json($response, 200);
}
}
Note: To get the response from the custom request, we have to set header Accept: application/json
.
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)