Site icon MageComp Blog

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.

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!

Exit mobile version