In this tutorial you will learn about the Laravel 9 Form Validation Tutorial with Example and its application with practical example.
In this Laravel 9 form validation example tutorial I’ll show you how to validate form data in laravel 9 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 9 form validation tutorial I’ll share various validation implementation and use case.
Laravel 9 Form Validation Tutorial
Generally Form validation is performed at server side, but can be performed at both the server and client side. In this laravel form validation tutorial we will be learning about laravel server side validations. In this tutorial, I’ll show you how to define laravel validation rules, how to validate form input and to display validation errors.
Laravel 9 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 9 Form Validation Example
In this step by step tutorial I’ll show you how to implement validations in laravel form.
- Download Laravel 9 Application
- Setup Database with App
- Create Model & Migration
- Create Form Routes
- Create Form Controller By Artisan Command
- Create Form Blade File
- Run Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 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 Model & Migration
Now, in this step we will create model and migration file for form. Please run the following command:
1 |
php artisan make:model Employee -m |
Once above command is executed there will be a migration file created inside database/migrations/ directory, just open create_employees_table.php migration file and update the function up() method as following:
1 2 3 4 5 6 7 8 9 10 11 |
public function up() { Schema::create('employees', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email'); $table->string('contact_no'); $table->string('age'); $table->timestamps(); }); } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Create Form 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 |
use App\Http\Controllers\FormController; Route::get('form', [FormController::class, 'index']); Route::post('store-form', [FormController::class, 'store']); |
Create Form Controller By Artisan Command
Next, we have to create a controller to display form and to handle form validation and submit operations. Lets Create a controller named FormController using command given below –
1 |
php artisan make:controller FormController |
Open the FormValidationController.php file and put the following code in it.
app/Http/Controllers//FormController.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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Employee; class FormController extends Controller { public function index() { return view('form'); } public function store(Request $request) { $validatedData = $request->validate([ 'name' => 'required', 'email' => 'required|unique:employees|max:255', 'age' => 'required', 'contact_no' => 'required|unique:employees|max:255', ]); $emp = new Employee; $emp->name = $request->name; $emp->email = $request->email; $emp->age = $request->age; $emp->contact_no = $request->contact_no; $emp->save(); return redirect('form')->with('status', 'Form Data Has Been validated and insert'); } } |
Create Form Blade File
Now, create form blade view file to display form and submit to database. So, Go to resources/views and create form.blade.php and update the following code into it:
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 |
<!DOCTYPE html> <html> <head> <title>Laravel 9 Example Form Validation</title> <meta name="csrf-token" content="{{ csrf_token() }}"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container mt-4"> @if(session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif <div class="card"> <div class="card-header text-center font-weight-bold"> <h2>Laravel 9 Form Validation Tutorial</h2> </div> <div class="card-body"> <form name="employee" id="employee" method="post" action="{{url('store-form')}}"> @csrf <div class="form-group"> <label for="exampleInputEmail1">Name</label> <input type="text" id="name" name="name" class="@error('name') is-invalid @enderror form-control"> @error('name') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> <div class="form-group"> <label for="exampleInputEmail1">Email</label> <input type="email" id="email" name="email" class="@error('email') is-invalid @enderror form-control"> @error('email') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> <div class="form-group"> <label for="exampleInputEmail1">Age</label> <input type="number" id="age" name="age" class="@error('age') is-invalid @enderror form-control"> @error('age') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> <div class="form-group"> <label for="exampleInputEmail1">Contact No</label> <input type="number" id="contact_no" name="contact_no" class="@error('contact_no') is-invalid @enderror form-control"> @error('contact_no') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </div> </body> </html> |
The following below code will display validation error message on blade view file:
1 2 3 |
@error('name') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/form