Laravel 11 Form Validation Example Tutorial | websolutioncode.com
Laravel 11 Form Validation Example Tutorial | websolutioncode.com

Laravel 11 Form Validation Example

Introduction:

In web development, form validation is a crucial aspect to ensure that the data submitted by users is accurate, secure, and meets the specified criteria. Laravel, a popular PHP framework, provides built-in features for form validation, making it easier for developers to handle user input validation efficiently. In this tutorial, we will explore how to perform form validation in Laravel 11.

Prerequisites: Before starting this tutorial, ensure you have the following prerequisites:

  • Basic knowledge of PHP and Laravel framework.
  • Laravel 11 installed on your system.
  • Composer installed to manage dependencies.

Setting Up Laravel 11 Project: If you haven’t installed Laravel 11 yet, you can do so using Composer. Open your terminal and run the following command:

composer create-project --prefer-dist laravel/laravel form-validation-tutorial

This command will create a new Laravel project named “form-validation-tutorial”.

Creating a Form: Let’s create a simple form to demonstrate form validation. Navigate to the resources/views directory and create a new file named contact.blade.php. Open this file and add the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Form</title>
</head>
<body>
    <h2>Contact Us</h2>
    <form method="POST" action="{{ route('submitForm') }}">
        @csrf
        <label for="name">Name:</label><br>
        <input type="text" id="name" name="name"><br>
        @error('name')
            <div>{{ $message }}</div>
        @enderror

        <label for="email">Email:</label><br>
        <input type="email" id="email" name="email"><br>
        @error('email')
            <div>{{ $message }}</div>
        @enderror

        <button type="submit">Submit</button>
    </form>
</body>
</html>

This form contains fields for name and email. We’ve also added Laravel’s CSRF protection and error messages for validation.

Creating a Route:

Next, let’s define a route to handle form submission. Open the routes/web.php file and add the following route definition:

use App\Http\Controllers\FormController;

Route::get('/contact', [FormController::class, 'showForm'])->name('contactForm');
Route::post('/submit-form', [FormController::class, 'submitForm'])->name('submitForm');

Creating a Controller:

Now, let’s create a controller to handle form submissions and validation. Run the following command to generate a controller:

php artisan make:controller FormController

Open the generated FormController.php file located in the app/Http/Controllers directory and add the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FormController extends Controller
{
    public function showForm()
    {
        return view('contact');
    }

    public function submitForm(Request $request)
    {
        $validatedData = $request->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|max:255',
        ]);

        // Process the form submission
        // ...

        return "Form submitted successfully!";
    }
}

In the submitForm method, we’re using Laravel’s validate method to validate the incoming form data based on specified rules.

Testing the Form: Now, you can test the form validation by accessing the /contact route in your browser. Fill out the form with valid or invalid data and submit it. Laravel will handle the validation and display error messages accordingly.

Conclusion:

In this tutorial, we’ve learned how to perform form validation in Laravel 11 using built-in features. Laravel’s validation functionality simplifies the process of validating user input, ensuring data integrity and security in web applications. By following the steps outlined in this tutorial, you can effectively implement form validation in your Laravel projects.

Leave a Reply