How Server Side Form Validation work in Laravel 8?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today in this tutorial i will show you Laravel 8 server side form validation from steps by steps. I will give you a example of laravel 8 form validation with error message in details on laravel 8 application. Here i will use custom error message to show form validation error message before submit the data into database.

Here I will show error message for each input field. Now we will use has() for checking the error message in laravel 8 application. Here, now i am going to show you a very simple and a very easy custom example of form validation.  So, you can simply use in your any laravel 8 project.

We need to use form validation before form submit. By data sanitization makes our website more secure. So we do it every time it is very good practice to use client side as well as server side form validation. So  now i will discuss about Laravel 8 form validation.

Step 1 : Create Routes

At first we need to add route on our project. Just add bellow code in your web.php file.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HomeController;
  
Route::get('user/create', [ HomeController::class, 'create' ]);
Route::post('user/create', [ HomeController::class, 'store' ]);

Step 2 : Create Controller

Now we need a new controller. After create new controller we will add two methods on this controller. One will just display blade file with get request data, and another one for post request to save data into database. Here i write validation for that, so now simply add both following method on it.

app/Http/Controllers/HomeController.php

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('test');
    }
    
    /**
     * 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.');
    }
}

Read Also : Laravel 8 CRUD Tutorial From Scratch

Step 3 : Create Blade File

Now we are in final step.  Now here we will create a test.blade.php file and here we will create a bootstrap simple form also with error validation message. So, let’s create following file and put bellow code here:

resources/views/test.blade.php


<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 form validation example - codingspoint</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 8 Form Validation Example - codingspoint</h1>
   
        @if(Session::has('success'))
        <div class="alert alert-success">
            {{ Session::get('success') }}
            @php
                Session::forget('success');
            @endphp
        </div>
        @endif
   
        <form method="POST" action="{{ url('user/create') }}">
  
            {{ csrf_field() }}
  
            <div class="form-group">
                <label>Name:</label>
                <input type="text" name="name" class="form-control" placeholder="Name">
                @if ($errors->has('name'))
                    <span class="text-danger">{{ $errors->first('name') }}</span>
                @endif
            </div>
   
            <div class="form-group">
                <label>Password:</label>
                <input type="password" name="password" class="form-control" placeholder="Password">
                @if ($errors->has('password'))
                    <span class="text-danger">{{ $errors->first('password') }}</span>
                @endif
            </div>
    
            <div class="form-group">
                <strong>Email:</strong>
                <input type="text" name="email" class="form-control" placeholder="Email">
                @if ($errors->has('email'))
                    <span class="text-danger">{{ $errors->first('email') }}</span>
                @endif
            </div>
   
            <div class="form-group">
                <button class="btn btn-success btn-submit">Submit</button>
            </div>
        </form>
    </div>
</body>
</html> 

Read Also : json_encode() is better then @json() in Blade Directive

Hope this laravel 8 form validation tutorial will help you.You can also follow us onĀ facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →