Laravel 8 Form Validation Example

WebSolutionStuff | Oct-10-2020 | Categories : Laravel

In this tutorial we will see laravel 8 form validation example, form validation in laravel is very common functionalities and it is use in each and every website to validate form field.

Here, We will use has function in session to check error message in laravel 8. using this example you can check simple form validation as well as you can create your own custom validtion in laravel 8. So,let's start

Step 1 : Add Route

We are adding route in web.php file for form validation tutorial in laravel 8.  


use Illuminate\Support\Facades\Route;

Route::get('user/create','App\Http\Controllers\[email protected]');
Route::post('user/store','App\Http\Controllers\[email protected]');



Step 2 : Create Controller

Now, we will create UserController to save data in database.So,copy below code in your controller.


namespace App\Http\Controllers;

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

class UserController extends Controller
    public function create()
    	return view('index');

    public function store(Request $request)
        $validatedData = $request->validate([
                'name' => 'required',
                'password' => 'required|min:8',
                'email' => 'required|email|unique:users'
            ], [
                'name.required' => 'Name is required',
                'password.required' => 'Password is required',
                'email.required' => 'Email is required'
        $validatedData['password'] = bcrypt($validatedData['password']);
        $user = User::create($validatedData);
        return back()->with('success', 'User Created Successfully !!');


Step 3 : Add Blade File

In this step we will create blade file and save name as index.blade.php

    <title>Laravel 8 Form Validation Example -</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="" rel="stylesheet">
    <div class="container">  
        <div class="row">
            <div class="col-md-12" style="margin-top: 30px;">
                <h1>Laravel 8 Form Validation Example -</h1>   
                <div class="alert alert-success">
                    {{ Session::get('success') }}
                <form method="POST" action="{{ url('user/store') }}">
                    <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">
                        <button class="btn btn-info btn-submit">Submit</button>


And finally you will get result like below image.



Featured Post

Follow us
facebooklogo github instagram twitter