Laravel 11 Form Validation Example Tutorial

By Hardik Savani April 16, 2024 Category : Laravel

In this example, I will show you how to add form validation in laravel 11 application.

Laravel 11 provides a request object to add form validation using it. We will use request->validate() for adding validation rules and custom messages. We will use the $errors variable to display error messages. I will show you a very simple step-by-step example of how to add form validation in the Laravel 11 application.

So, let's see the below example for adding form validation.

laravel 11 form validation

Step for Laravel 11 Form Validation with Error Message

  • Step 1: Install Laravel 11
  • Step 2: Create Controller
  • Step 3: Create Routes
  • Step 4: Create Blade File
  • Run Laravel App

Step 1: Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Controller

In this step, we will create a new `FormController` for adding form validation. In this controller, we will add two methods called `create()` and `store()`. So let's create a new controller using the following command.

php artisan make:controller FormController

Next, let's update the following code in that file.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class FormController extends Controller
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function create(): View
        return view('createUser');
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function store(Request $request): RedirectResponse
        $validatedData = $request->validate([
                'name' => 'required',
                'password' => 'required|min:5',
                'email' => 'required|email|unique:users'
            ], [
                'name.required' => 'Name field is required.',
                'password.required' => 'Password field is required.',
                'email.required' => 'Email field is required.',
                '' => 'Email field must be email address.'
        $validatedData['password'] = bcrypt($validatedData['password']);
        $user = User::create($validatedData);
        return back()->with('success', 'User created successfully.');

Step 3: Create Routes

Furthermore, open `routes/web.php` file and add the routes to manage GET and POST requests for calling views and adding form validation.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FormController;
Route::get('users/create', [ FormController::class, 'create' ]);
Route::post('users/create', [ FormController::class, 'store' ])->name('');

Step 4: Create Blade File

Now, here we will create createUser.blade.php file and here we will create a bootstrap simple form with error validation messages. So, let's create the following file:


<!DOCTYPE html>
    <title>Laravel 11 Form Validation Example -</title>
    <link href="" rel="stylesheet" crossorigin="anonymous">
    <link rel="stylesheet" href="" />
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3"><i class="fa fa-star"></i> Laravel 11 Form Validation Example -</h3>
        <div class="card-body">
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}
            <!-- Way 1: Display All Error Messages -->
            @if ($errors->any())
                <div class="alert alert-danger">
                    <strong>Whoops!</strong> There were some problems with your input.<br><br>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
            <form method="POST" action="{{ route('') }}">
                {{ csrf_field() }}
                <div class="mb-3">
                    <label class="form-label" for="inputName">Name:</label>
                        class="form-control @error('name') is-invalid @enderror" 
                    <!-- Way 2: Display Error Message -->
                        <span class="text-danger">{{ $message }}</span>
                <div class="mb-3">
                    <label class="form-label" for="inputPassword">Password:</label>
                        class="form-control @error('password') is-invalid @enderror" 
                    <!-- Way 3: Display Error Message -->
                    @if ($errors->has('password'))
                        <span class="text-danger">{{ $errors->first('password') }}</span>
                <div class="mb-3">
                    <label class="form-label" for="inputEmail">Email:</label>
                        class="form-control @error('email') is-invalid @enderror" 
                        <span class="text-danger">{{ $message }}</span>
                <div class="mb-3">
                    <button class="btn btn-success btn-submit"><i class="fa fa-save"></i> Submit</button>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:



laravel 11 form validation output

I hope it can help you...