Laravel Form Requests

Hossam Soliuman
2 min readJul 10, 2024

--

Title: Mastering Laravel Form Requests for Robust Validation

Laravel’s Form Requests are a powerful way to encapsulate validation logic within custom request classes. This feature not only keeps your controllers clean but also provides a more organized and reusable way to handle validation. In this post, we’ll explore the basics of Form Requests, how to create them, and how to leverage their capabilities for better validation in your Laravel applications.

Why Use Form Requests?

  1. Separation of Concerns: By moving validation logic to its own class, your controllers stay focused on handling requests and responses.
  2. Reusability: Form Requests can be reused across different controllers or methods.
  3. Cleaner Code: It makes your codebase cleaner and easier to maintain.

Creating a Form Request

To create a Form Request, use the Artisan command:

php artisan make:request StorePostRequest

This command creates a new request class in the app/Http/Requests directory.

Defining Validation Rules

Open the generated StorePostRequest class and define your validation rules in the rules method:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

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

In this example, the rules method returns an array of validation rules for the fields title, content, and tags.

Customizing Validation Messages

You can customize the validation messages by overriding the messages method in your Form Request:

public function messages()
{
return [
'title.required' => 'The title is required.',
'content.required' => 'The content cannot be empty.'
];
}

When the store method is called, Laravel automatically validates the incoming request using the rules defined in StorePostRequest. If the validation fails, it redirects back to the previous page with the validation errors. If the validation passes, the validated method returns the validated data, which you can use to create a new post.

Advanced Features

  1. Authorization Logic: The authorize method in the Form Request class can be used to check if the user is authorized to make this request. Return true to allow the request, or false to deny it.
  2. After Validation Hook: You can define an after method to perform additional operations after validation, such as modifying the validated data or performing additional checks.

Conclusion

Laravel Form Requests provide a structured and powerful way to handle request validation. By using Form Requests, you keep your controllers clean, improve code reusability, and maintain a clear separation of concerns. Start incorporating Form Requests into your Laravel applications to leverage their full potential and write more maintainable and robust code.

--

--

Hossam Soliuman

Software Engineer | Web Developer | Backend Developer | PHP | Laravel | Freelancer