Using FormRequest in Laravel 11

laravel 11 foundation formrequest
09 August 2025

Introduction to FormRequest in Laravel 11



Hello, dear friend! Today we want to talk about FormRequest in Laravel 11. FormRequest is a powerful functionality in Laravel that allows you to easily validate user input data. This feature enables you to separate validation logic from controllers and make your code cleaner and easier to maintain.



In fact, when you use FormRequest, you will find a new class that can handle all the validation rules and criteria you define. This means that your controllers will only handle data processing tasks and there will be no need to manage validation logic.



To use FormRequest, you first need to create a new request class. For example, let’s say we want to validate user registration data. We can use a command to create a new class named RegisterRequest, in which we will write all the validation rules.



After creating this class, we can easily use it in our controller. Just make sure that the type of input parameter in the controller changes to the new class we created. This will enable automatic validation and if the data isn't valid, an appropriate response to the user will be displayed.


Code Example for Using FormRequest


php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RegisterRequest extends FormRequest
{
public function authorize()
{
return true;
}

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

Description of the Code


namespace App\Http\Requests;
This line defines the namespace of our class. Here we're using the namespace App\Http\Requests.


use Illuminate\Foundation\Http\FormRequest;
Here we include the main FormRequest class that we use in Laravel.


class RegisterRequest extends FormRequest
In this line, we define a new class named RegisterRequest that inherits from FormRequest.


public function authorize()
This method specifies whether the user is authorized to make this request or not. Here we assume it's true, meaning all users are authorized to use this request.


public function rules()
In this method, we define the validation rules. These rules specify what types of data the user must provide and what counts as valid.


In this example:



  • 'name' => 'required|string|max:255' The name is required, must be a string, and has a maximum length of 255 characters.

  • 'email' => 'required|string|email|max:255|unique:users' The email is required, must be a string in email format, and must be unique in the users' table.

  • 'password' => 'required|string|min:8|confirmed' The password is required, must be a string, at least 8 characters long, and must match the confirmation field.


FAQ

?

What is FormRequest in Laravel?

?

How can I create a FormRequest in Laravel?

?

Where do I define the validation rules?

?

Can I use FormRequests in controllers?