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:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=<DATABASE NAME>
DB_USERNAME=<DATABASE USERNAME>
DB_PASSWORD=<DATABASE PASSWORD>
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;
<?php
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.
<?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)
{
$request->validate([
'title' => 'required|unique:posts|max:255',
'discription' => 'required',
]);
Post::create($request);
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>
<html>
<head>
<title>Laravel form validation example - codingdriver.com</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="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Laravel form validation Example</h1>
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
@php
Session::forget('success');
@endphp
</div>
@endif
<form method="POST" action="{{ route('post') }}">
{{ csrf_field() }}
<div class="form-group">
<label>Title:</label>
<input type="text" name="title" class="form-control" placeholder="Name">
@if ($errors->has('title'))
<span class="text-danger">{{ $errors->first('title') }}</span>
@endif
</div>
<div class="form-group">
<strong>Discription:</strong>
<textarea name="discription" class="form-control" placeholder="Discription"><textarea>
@if ($errors->has('discription'))
<span class="text-danger">{{ $errors->first('discription') }}</span>
@endif
</div>
<div class="form-group">
<button class="btn btn-success btn-submit">Submit</button>
</div>
</form>
</div>
</body>
</html>
Now you can check the validations errors in laravel blade file. For more follow us on twitter.