Laravel 10 Validate Internet Protocol (IPv6) Tutorial

Last Updated on by in Laravel

Laravel IPv6 validation example; Since IPv6 (Internet Protocol version 6) is the newest addition to Internet Protocol, It is way better than IPv4. It offers distinctive IP addresses essential for Internet-enabled devices to communicate.

In this tutorial, you will find out how to add validation for the IPv6 input field in the Laravel simple app. To integrate the validation for IPv6, we will be using Laravel’s default request method along with Bootstrap 5.

To incorporate the validation in IPv6, we will begin with a basic Laravel app installation, generate and set up a laravel controller particularly for IPv6 validation.

Then move on to create routes for handling the request, and build the form using Bootstrap and test the laravel IPv6 server-side validation.

How to Validate IPv6 (Internet Protocol Version 6) in Laravel 10

  • Step 1: Install Laravel App
  • Step 2: Generate Controller
  • Step 3: Set Up Controller
  • Step 4: Define Routes
  • Step 5: Build Form with Bootstrap
  • Step 6: Test IPv6 Validation

Install Laravel App

In order to install the fresh Laravel application, make sure to configure Composer tool on your development machine.

Open the terminal, on the command-prompt hit the given command and execute the command to download the app.

composer create-project --prefer-dist laravel/laravel laravel-boom

Once the app is generated, move inside the app directory.

cd laravel-boom

Generate Controller

A controller handles the core logic of Laravel apps, and it’s just a simple file where all the feature-related code resides.

You may create it manually or simply invoke the suggested below command from the command prompt.

php artisan make:controller ProductController

Now the ProductController file has been created, you now have to follow the subsequent step.

Set Up Controller

Get into the app\Http\Controllers\ directory and look for ProductController.php file. In this file you have to place the suggested code.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;

class ProductController extends Controller
{
    public function ipv6form()
    {
        return view('index');
    }

    public function validateForm(Request $request)
    {
        $request->validate([
            'ip_address' => 'required|ipv6',
        ]);

        dd('Form submitted successfully!');
    }
}

The ProductController class, will have the two functions and handle the view for the form and required validation for the IPv6 input field.

Define Routes

To build the routes for handling the form and validation, get inside the routes/ folder and add the given code into the web.php file.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/my-form', [ProductController::class, 'ipv6form']);

Route::post('/submit-form', [ProductController::class, 'validateForm'])->name(
    'validate.ip'
);

Build Form with Bootstrap

Now, you need to go to resources/views/ folder, inside this folder you have create index.blade.php file and insert the suggested code into the view file.

<!DOCTYPE html>
<html>

<head>
    <title>Laravel IPv6 Validation</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
    <div class="container mt-5">
        @if (count($errors) > 0)
            <div class="alert alert-danger">
                @foreach ($errors->all() as $error)
                    {{ $error }} <br>
                @endforeach
            </div>
        @endif
        <form action="{{ route('validate.ip') }}" method="post">
            @csrf
            <div class="mb-3">
                <label>IP Address</label>
                <input type="text" class="form-control" name="ip_address">
            </div>

            <div>
                <button class="btn btn-danger">Send</button>
            </div>
        </form>
    </div>
</body>

</html>

Add the bootstrap CSS link within the head section to allow building the form using bootstrap form UI component.

Define the form tag, pass the route that makes the post request and evoke the server side validation for IPv6 form field.

Test IPv6 Validation

In this segment, we have to just run the laravel development server. Hence, execute the given command to run the app.

php artisan serve

You can test or view the app through given url:

http://127.0.0.1:8000/my-form

Laravel Validate Internet Protocol (IPv6) Tutorial

Conclusion

If you’ve never implemented IPv6 validation in Laravel before, you may have discovered how to include the required validation for IPv6 through Laravel’s inbuilt mechanism.

This guide walked you through the simple process and undoubtedly helped you decipher the know-how for validating the IPv6 in Laravel.