In this tutorial, you will learn Laravel Form Validation with Error Messages Example step by step process. Laravel provides a powerful validation system that includes customizable error messages. Using In Laravel, you can easily validate form input data and display error messages to the user using Laravel’s built-in form validation feature.
Here’s an example of how to use Laravel form validation with error messages.
Displaying Error Messages in laravel
First, you need to will create two routes one for showing form and another is displaying error messages in Laravel.
Step 1: Make Routes
use App\Http\Controllers\PostController;
Route::get('posts', 'PostController@create');
Route::post('posts', 'PostController@store')->name('posts');
Step 2: Create Controller
Now we will generate a new controller to handle the validation error messages request and showing them:
php artisan make:controller PostController
Now open your new generated controller App\Http\Controllers\PostController and put the below code on it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts');
}
/**
* 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 3: Create Blade File
Next, we need to create a posts.blade.php blade view file for showing the form.
Now open the resources/views/posts.blade.php file and put 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('posts') }}">
{{ 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>
Learn Here: How to Show Input Boxes to Red Laravel Validations Example
Now you can check the validations errors in laravel blade file. For more follow us on twitter.
1 thought on “Laravel Form Validation with Error Messages Example”