Form Validation Example in Laravel Tutorial

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog, I will show you how to use form validation in laravel application. Here we can use laravel default validation rules as like required, email, unique, numeric, date, ip, in_array, it, ite, max, min, image, mimes etc. So here i will use laravel 8 form validation also with display error messages on the view file.

So now here i will define the custom error messages in laravel form validation. So we can display the error message also with each field. So here i will use has() for checking is error message in laravel application.

Now here i will explain the simple example of form validation.So you can simply use it in your laravel project.

Step: 1 Create Route

Now here we are easy example of validation in laravel application so just need to add following both route in your web.php file. So now we need to create route for Form Validation layout file

following path:/routes/web.php

Route::get('client/create', 'ClientController@create');
Route::post('client/store', 'ClientController@store')->name('client.store');
Step: 2 Create Controller

Now in this step now we need to create new controller as named ClientController, So need to run bellow command for generate the new controller

php artisan make:controller ClientController

Now in this step, on this controller will manage form validation layout in the bellow content in controller file. In the following file path

following path:/app/Http/Controllers/ClientController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Client;

class ClientController extends Controller
{  /**
  * Show the application dashboard.
  *
  * @return \Illuminate\Http\Response
  */
  public function create()
  {
  	return view('createClient');
  }
  /**
  * Show the application dashboard.
  *
  * @return \Illuminate\Http\Response
  */
  public function store(Request $request)
  {
  	  $request->validate([
	        'name' => 'required',
	        'password' => 'required|min:5',
	        'email' => 'required|email|unique:users'
	    ], [
	        'name.required' => 'Name is required',
	        'password.required' => 'Password is required'
	    ]);

  	  $input = $request->all();
  	  $input['password'] = bcrypt($input['password']);
  	  Client::create($input);

  	  return back()->with('success', 'User created successfully.');
  }
}
Step: 3 Create Blade Files

Now we are in last step, so let’s create a createClient.blade.php (resources/views/createClient.blade.php) for layout and then Form Validation design code here and put following code

following path:/resources/views/createClient.blade.php

<!DOCTYPE html>
<html>
<head>
   <title>Form Validation Example in Laravel 6 Tutorial - CodingsPoint.com</title>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
</head>
<body class="bg-dark">
<div class="container">
   <div class="row">
      <div class="col-md-8 offset-md-2">
         <div class="card mt-5">
            <div class="card-header">
               <h5>Form Validation Example in Laravel 6 Tutorial - CodingsPoint.com </h5>
            </div>
            <div class="card-body">
               <form action="{{ route('client.store') }}" method="post">
                  @csrf
                  <div class="row">
                     <div class="col-md-12">
                        <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>
                  </div>
                  <div class="row">
                     <div class="col-md-12">
                        <div class="form-group">
                           <label>Email</label>
                           <input type="email" name="email" class="form-control" placeholder="Email">
                           @if ($errors->has('email'))
                                <span class="text-danger">{{ $errors->first('email') }}</span>
                           @endif
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-12">
                        <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>
                  </div>
                  <div class="row">
                     <div class="col-md-12">
                        <div class="form-group">
                           <button class="btn btn-success" type="submit">Submit</button>
                        </div>
                     </div>
                  </div>
               </form>
            </div>
         </div>
      </div>
   </div>
</div>
</body>
</html>

So now we are ready to run our example . Now need to run bellow command for quick run:

php artisan serve

Now we can open bellow URL on our browser:

http://localhost:8000/client/create

Read Also: Laravel 8 Mobile Number Verification Tutorial

Thank you read.Hope it will help you. Also follow 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 →