laravel 11 form validation example tutorial, show you how to add validation in Laravel along with Custom Error Messages, explained step by step. Laravel offers a robust validation mechanism with the flexibility to customize error messages. With Laravel, validating form input data and presenting clear error messages to users is straightforward thanks to its built-in form validation feature.
Laravel 11 Form Validation Example
You can validate your form and display messages in Laravel 11 application using the following steps:
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>
Now you can check the validations errors in laravel blade file.