How to Create Form Request Validation in Laravel 10

Websolutionstuff | Feb-23-2024 | Categories : Laravel

Hey there! Today, I want to talk to you about a super useful feature in Laravel 10 called form request validation. If you're building web applications with Laravel, you know how important it is to validate the data your users submit through forms.

It helps ensure that your application stays secure and your data remains consistent. Form request validation in Laravel 10 makes this process incredibly easy and organized. Instead of scattering your validation logic throughout your controllers, you can encapsulate it neatly into dedicated form request classes.

These classes handle the validation rules for each of your forms, keeping your code clean and maintainable.

So, let's see how to create form request validation in laravel 10, laravel 10 form request validation, and how to create form request validation in laravel 8/9/10, laravel validation using request.

In this guide, I'll walk you through the step-by-step process of creating form request validation in Laravel 10.

Step 1: Install Laravel 10

First, make sure you have Laravel 10 installed. If not, you can install it via Composer by running:

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

 

Step 2: Create a Form Request

Laravel provides a convenient way to validate incoming HTTP request data using form requests. To create a form request, run the following Artisan command:

php artisan make:request MyFormRequest

Replace MyFormRequest with your desired name for the request.

 

Step 3: Define Validation Rules

Open the generated form request file located at app/Http/Requests/MyFormRequest.php. Inside the rules() method, define the validation rules for your form fields. For example:

public function rules()
{
    return [
        'name' => 'required|string|max:255',
        'email' => 'required|email|unique:users',
        'password' => 'required|string|min:8|confirmed',
    ];
}

 

Step 4: Authorize the Request

You can also define the authorization logic in the authorize() method if needed. For example, allowing only authenticated users to submit the form

public function authorize()
{
    return Auth::check();
}

 

Step 5: Handle Validation Errors

Laravel automatically handles validation errors and redirects the user back to the previous page with error messages flashed to the session. You can display these error messages in your views using Blade directives like @error('field').

 

Step 6: Use the Form Request in the Controller

To use the form request in your controller, simply type-hint it in the controller method where you handle the form submission. For example

use App\Http\Requests\MyFormRequest;

public function store(MyFormRequest $request)
{
    // The request has passed validation; continue processing
}

That's it! You've now successfully created form request validation in Laravel 10.

 


You might also like:

Recommended Post
Featured Post
How to Deploy Laravel on Heroku with Database
How to Deploy Laravel on Herok...

In this article, we will see how to deploy laravel on heroku with database. Heroku is a cloud platform as...

Read More

Feb-12-2021

How to Get Random Record in Laravel 10
How to Get Random Record in La...

Here you will learn how to get random records from DB in laravel using the inRandomOrder() method. Explore an...

Read More

Nov-10-2023

Laravel 8 Image Upload Validation
Laravel 8 Image Upload Validat...

In tutorial we will see how to validate laravel 8 image upload validation. In laravel 7/8 you can validate image using t...

Read More

Dec-15-2021

How To Validate 10 Digit Mobile Number Using Regular Expression
How To Validate 10 Digit Mobil...

In This small tutorial, I will explain to you how to validate a 10-digit mobile number using regular expressions in...

Read More

Jun-15-2020