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

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago