Laravel 9 Validation Error Messages Tutorial with Example

Laravel Validation Error Messages Example; In this tutorial you will learn how to use laravel 9 form validation Example for showing the Error Messages. We will show you how to display error messages in laravel.

Blow example we will display error message in blade file with each field using has() for checking error message in laravel 9. Checkout how the controller send errors and how we display error messages in laravel blade files.

Step 1: Install Laravel app

First at all, open your terminal and run nthe following command for creating new laravel app.

composer create-project laravel/laravel laravel-app --prefer-dist

Step 2: Setup Database

This step explains how to make database connection by adding database name, username and password in .env config file of your project:


Step 3: Create Routes

First we need to create routes for displaying errors. So, open the routes/web.php file and update the below routes on it;

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

Route::get('post', [PostController::class, 'create'])->name('post');
Route::post('post', [PostController::class, 'store'])->name('generate-pdf');

Step 4: Create Controller

Next, you need to create a controller using the following command:

php artisan make:controller PostController

Open and update the below code in app\Http\Controllers\PostController.php.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function create()
        return view('create-post');
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function store(Request $request)
           'title' => 'required|unique:posts|max:255',
           'discription' => 'required',
        return back()->with('success', 'Post created successfully.');

Step 5: Create Blade File

Now here we will create create-post.blade.php file inside the resources/views direoctry. After that open the resources/views/create_post.blade.php file and update the below code on it;

<!DOCTYPE html>
    <title>Laravel form validation example -</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="//" rel="stylesheet">
    <div class="container">
        <h1>Laravel form validation Example</h1>
        <div class="alert alert-success">
            {{ Session::get('success') }}
        <form method="POST" action="{{ route('post') }}">
            {{ csrf_field() }}
            <div class="form-group">
                <input type="text" name="title" class="form-control" placeholder="Name">
                @if ($errors->has('title'))
                    <span class="text-danger">{{ $errors->first('title') }}</span>
            <div class="form-group">
                <textarea name="discription" class="form-control" placeholder="Discription"><textarea>
                @if ($errors->has('discription'))
                    <span class="text-danger">{{ $errors->first('discription') }}</span>
            <div class="form-group">
                <button class="btn btn-success btn-submit">Submit</button>

Now you can check the validations errors in laravel blade file. For more follow us on twitter.

Leave a Comment