How To

Laravel: How to Add Form Validation in the Request Controller

Hello Laravel Friends,

Today I will be explaining How to Apply Form Validation in the Request Controller in Laravel.

Validation is the important aspect while taking data from users. In Laravel you can easily validate using a request controller. To perform validation you need to have the following requirements.

  • PHP version greater than 7.0
  • A fresh laravel project

Once you are done with the above requirements, you can start implementing the below steps.

Let’s start

Steps to Add Form Validation in the Request Controller in Laravel:

Here we took the user registration form as an example. Here we will validate the name, email, password, and confirm password fields in the request.

Step 1: First, we will create a route in the route/web.php file for user registration post requests. In this request, we will pass name, email, password, and confirm password as body parameters.

use App\Http\Controllers\UserController;
Route::post('/registration', [UserController::class, 'userRegistration']);

Step 2: Then after, we will create a User controller for getting requests for user registration post route. Execute the following command to create the UserController.php file in your project root directory.

$ php artisan make:controller UserController

The UserControlller.php file is now created in your project App/Http/Controller directory. 

Step 3: Now create userRegistration function 

public function userRegistration(UserRequest $request)                 
{                                                                                  
 // @Class UserRegistration performs the validation of this request.   
 // do something here...                                              
}                                                                    

Step 4: Then create the UserRequest.php file for the validation request. To create UserRequest.php execute the following command

$ php artisan make:request UserRequest

The UserRequest.php file is created in your project App/Http/Request directory.

Step 5: For validating the request add rule in the rules function defined in the UserRequest.php file.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */    public function rules()
    {
        return [
            "name" => 'required',
            "email" => 'required|email',
            "password" => 'required|confirmed'
        ];
    }
}

Step 6: If there is an incorrect input, an error will be generated. You can get an error message with the following code in your blade file.

@if($errors->any())

{!! implode('', $errors->all('<div style="color:red">:message</div>')) !!}

@endif

Result:

After implementing the above code, validation has been successfully applied to the user registration form.

Conclusion:

This way you can easily validate form input in the request controller in Laravel. For any custom development Hire Laravel Developer.

Share this tutorial with other Laravel developers and stay updated to learn more with us.

Happy Coding!

Click to rate this post!
[Total: 7 Average: 4.3]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer 🏅 with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket 🏏.

Recent Posts

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

2 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

5 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

6 days ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

7 days ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

7 days ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

1 week ago