Typically forms are the best tools for getting information from site visitors, and a form may have one or more than one input element.
This tutorial will explain how to create a form input field for IPv6 and show you how to validate IPv6 address input in the Laravel application.
Throughout this laravel IPv6 validation example, we will show you various steps to help you integrate IPv6 validation in the laravel app.
How to Implement IPv6 Validation in Laravel 9
- Step 1: Download Laravel Project
- Step 2: Create Controller
- Step 3: Register Routes
- Step 4: Create View
- Step 5: Start Laravel App
Download Laravel Project
If you have set up the composer, type the command on the console and execute the command to install the app.
composer create-project --prefer-dist laravel/laravel laravel-demo
Get inside the project.
cd laravel-demo
Create Controller
In this step, you require to create the controller file, so use the php artisan command with make:controller attribute followed by the controller name to generate the new controller.
php artisan make:controller UserController
In the controller class, add two functions. Respectively, one function helps to show the view file in the browser, whereas the other function handles the ipv6 validation through the $request object.
Open and insert code in app\Http\Controllers\UserController.php file.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
class UserController extends Controller
{
public function create()
{
return view('home');
}
public function form(Request $request)
{
$request->validate([
'ipv6_val' => 'required|ipv6'
]);
dd('completed');
}
}
Register Routes
In the previous step, we generated and define the controller, now we have to import the controller in routes file and create the routes.
Hence, open and insert code in routes/web.php file.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::get('/user-form', [UserController::class, 'create']);
Route::post('/validate-form', [UserController::class, 'form'])->name('validate.form');
Create View
In the last step, we will use the Bootstrap CDN link for creating the form ui also for showing the validation error message.
First, create the home.blade.php file then add code in resources/views/home.blade.php file.
<!DOCTYPE html>
<html>
<head>
<title>IPv6 Validation in Laravel</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
@if (count($errors) > 0)
<div class="alert alert-danger alert-dismissible">
@foreach($errors->all() as $error)
{{ $error }} <br>
@endforeach
</div>
@endif
<form action="{{ route('validate.form') }}" method="post">
@csrf
<div class="form-group mb-3">
<label>ipv6 Address</label>
<input type="text" class="form-control" name="ipv6_val">
</div>
<div class="d-grid">
<button class="btn btn-dark">Submit</button>
</div>
</form>
</div>
</body>
</html>
View App in Browser
Again type the php artisan command with serve tag to run the laravel app, use the given command to start the app.
php artisan serve
Here is the url that will help you open the app.
http://127.0.0.1:8000/user-form
Summary
In this tutorial, you have learned how to validate Internet Protocol version 6 in the laravel application.
It is the most recent version of the Internet protocol that is generally used as the communications protocol. It not just allows a location mechanism for networks or computers but also grants identification.