Hello Devs, 

In this tutorial, we will learn Laravel 8 Form Validation Example

You will know how to define laravel validation rules, how to validate form input, and to display validation errors.

Follow this step-by-step guide below. 

Step 1 : Install Laravel 8

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

Step 2 : Database Configuration


DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)

Step 3: Add Resource Route


use App\Http\Controllers\HomeController;

Route::get('user/create', [ HomeController::class, 'create' ]);
Route::post('user/store', [ HomeController::class, 'store' ]);

Step 4: Add Controller

php artisan make:controller HomeController

create seven methods by default as bellow methods

  1. create()
  2. store()

copy the below code and paste it on the HomeController.php file



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class HomeController extends Controller
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function create()
        return view('createUser');

     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function store(Request $request)
        $validatedData = $request->validate([
                'name' => 'required',
                'password' => 'required|min:5',
                'email' => 'required|email|unique:users'
            ], [
                'name.required' => 'Name is required',
                'password.required' => 'Password is required'
        $validatedData['password'] = bcrypt($validatedData['password']);
        $user = User::create($validatedData);
        return back()->with('success', 'User created successfully.');

Step 5: Add Blade Files


<!DOCTYPE html>
    <title>Laravel 8 Form Validation Example - rathorji.in</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 rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" />
    <div class="container">
        <div class="row mt-5">
            <div class="col-8 offset-2 mt-5">
                <div class="card">
                    <div class="card-header bg-info">
                        <h3 class="text-white">Laravel 8 Form Validation Example - rathorji.in</h3>
                    <div class="card-body">
                        <div class="alert alert-success">
                            {{ Session::get('success') }}
                        <form method="POST" action="{{ url('user/store') }}">
                            {{ csrf_field() }}
                            <div class="form-group">
                                <input type="text" name="name" class="form-control" placeholder="Name">
                                @if ($errors->has('name'))
                                    <span class="text-danger">{{ $errors->first('name') }}</span>
                            <div class="form-group">
                                <input type="password" name="password" class="form-control" placeholder="Password">
                                @if ($errors->has('password'))
                                    <span class="text-danger">{{ $errors->first('password') }}</span>
                            <div class="form-group">
                                <input type="text" name="email" class="form-control" placeholder="Email">
                                @if ($errors->has('email'))
                                    <span class="text-danger">{{ $errors->first('email') }}</span>
                            <div class="form-group text-center">
                                <button class="btn btn-success btn-submit">Save</button>

run the below command for a quick run:

php artisan serve

open bellow URL on your browser:


May this example help you.