Create a Form Request in Laravel

Form validation is one of the most important aspects of any web application. In Laravel, there are several ways to validate incoming form requests. Learn how to create a form request in Laravel with attributes and messages.

Create a Form Request in Laravel

Laravel provides a way to move request validation logic into a separate class rather than placing it inside a controller. In this post, we are going to create a Laravel form request and add request attributes and messages to the form request class.

 

What is a Form Request in Laravel?

A form request in Laravel is a dedicated class responsible for handling data validation before saving it to the database. Laravel offers several validation rules to validate a form request and ensure data is in the correct format before using it. In addition to default form requests, Laravel also offers a way to define custom form request classes.

 

How to Create a Custom Form Request in Laravel?

Creating a form request has several advantages, such as helping to keep controllers focused on business logic by moving the request validation to a separate class. This results in cleaner controllers, reusable validation logic and better organization of code, making the Laravel application maintainable. We are going to create a form request in a Laravel class, which keeps the validation logic separate from the controller.

 

Step 1: Create Laravel Form Request

The first step to create a form request is using an artisan command that will create a form request class in the app/Http/Requests directory of the Laravel application. We are going to create a class with named StorePostRequest.

php artisan make:request StorePostRequest
 

Step 2: Define Request Validation Rules

After we have created our form request class, we need to define some validation rules. The validation rules can be defined in the rules() method of the request class. So whenever we use this class in our controller, these validation rules will be applied. To define validation rules in a form request, open the class file and add the rules like in the code snippet below:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePostRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

public function rules(): array
{
return [
'title' => 'required|string|max:255',
'content' => 'required',
];
}
}
 

Step 3: Use The Form Request Class in The Controller

Now, after we have added validation rules to the form request class, we can use it in our controller by injecting it as a type-hint parameter. Notice how our controller method does not have a validation code; instead, the validation is taking place in the form request class.

  • Use the form request class at the top of the controller.
  • In the store() method of the controller, inject the class with the type-hint StorePostRequest $request parameter.
  • Use the form request validation with the $request->validated() method.
use App\Http\Requests\StorePostRequest;

class UserController extends Controller
{
public function store(StorePostRequest $request)
{
$validated = $request->validated();

Post::create($validated);

return redirect()->back()->with('success', 'Post created successfully.');
}
}
 

Step 4: Define Custom Validation Messages

We can also define custom validation error messages using the messages() method of the form request class. This method returns an array of messages; each key has the name of the form field concatenated with the validation rule, such as title.required means if the title field is empty, the message in this array will be used as a validation error message.

public function messages(): array
{
return [
'title.required' => 'Please enter post title.',
'content.required' => 'Please enter content of post.',
];
}
 

Step 5: Define Custom Attribute Names

Laravel also allows us to add custom attribute names to our form request class. These attributes can be added using the attributes() method of the class, which returns an array of attributes. For example, the attribute title will now show "Post title" instead of "title field" to users.

public function attributes(): array 
{
return [
'title' => 'Post title',
'content' => 'Post content',
];
}

The authorize() method of the request class determines whether the user can perform the validation action or not. We can use this method to restrict which users can perform an action.

public function authorize()
{
return true;
}

Form requests are one of the powerful features of Laravel for organizing the validation logic rather than having bloated controllers. We demonstrated how to create and use a Laravel form request to separate the validation logic from controllers to improve the readability and scalability of the application.