In this tutorial you will learn about the Laravel 8 Form Validation Example and its application with practical example.
In this Laravel 8 form validation example tutorial I’ll show you how to validate form data in laravel 8 application. In this laravel form validation tutorial you will learn to validate laravel form with built in laravel validation available in laravel. In this Laravel 8 form validation tutorial I’ll share various validation implementation and use case.
Laravel comes many built-in validation rules that can be easily implemented. In this example, we have used some of following validation rules –
required:- It set input field is required.
min:- Validate minimum character length.
max:- Validate maximum character length.
email: It set input must be a valid email address.
unique: It checks for unique value against a database column.
numeric: Input value must numeric.
same:- It validates two input fields value must be same.
Laravel 8 Form Validation Example
In this step by step tutorial I’ll demonstrate you how to implement validations in laravel 8 form. Please follow the instruction given below:
Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel larablog |
Configure Database In .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=larablog DB_USERNAME=root DB_PASSWORD= |
Create Routes:
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 |
<?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' ]); |
Create Controller:
Next, we have to create a controller to display form and to handle form validation and submit operations. Lets Create a controller named HomeController using command given below –
1 |
php artisan make:controller <strong><span class="heads">HomeController</span></strong> |
Open the HomeController.php file and put the following code in it.
app/Http/Controllers/HomeController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?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('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.'); } } |
Create Blade File:
Now, create form blade view file to display form and submit to database. So, Go to resources/views and create createUser.blade.php and update the following code into it:
resources/views/createUser.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<!DOCTYPE html> <html> <head> <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="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <h1>Laravel 8 Form Validation Example</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> |