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 Check Occupied Disk Space In Laravel
How To Check Occupied Disk Spa...

Many times we need requirements to check the occupied disk space of the server on the admin side and we are checking man...

Read More

Jul-29-2020

Datatables Expand/Collapse Columns
Datatables Expand/Collapse Col...

In this article, we will see how to expand/collapse columns in datatable. The Datatables API has a number of method...

Read More

Jun-05-2022

How To Create Multi Language Website In Laravel 9
How To Create Multi Language W...

In this article, we will see how to create a multi language website in laravel 9. In this example, we will see the...

Read More

Apr-20-2022

How to Search Object by ID and Remove It from JSON Array In JavaScript
How to Search Object by ID and...

In this example we will see how to search object by id and remove it from json array in javascript. we need to add ...

Read More

May-26-2021