Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
LaravelPHPJune 20, 20263 min read

Form validation in Laravel made easy: A Practical Guide

Form validation in Laravel is simple when you move logic out of your controllers. Learn how to use FormRequest classes to keep your code clean and dry.

LaravelPHPWeb DevelopmentValidationBest PracticesTutorial
Close-up of a person signing a document on a wooden table, emphasizing detail and focus.

Last month, I jumped into a legacy project where every single controller method began with 30 lines of if statements checking for null values or incorrect email formats. It was a nightmare to debug, and adding a single field meant touching code in three different places. If you're still manually validating input in your controllers, you're making your life harder than it needs to be.

The Right Way to Handle Form Validation in Laravel

The secret to clean code isn't adding more packages; it's using the features Laravel gives you out of the box. Specifically, the FormRequest class is your best friend. It acts as a gatekeeper, ensuring that your controller only ever sees data that has already been sanitized and verified.

When you start a new project, you’re likely already familiar with the basics of Laravel routing and controllers: A Beginner's Guide to MVC. But as those controllers grow, they become dumping grounds for logic that doesn't belong there. Moving validation into a dedicated class is the first step toward a professional architecture.

Creating Your First FormRequest

Instead of using $request->validate() inside your controller, generate a dedicated request class using Artisan:

Bash
php artisan make:request StoreUserRequest

This creates a file in app/Http/Requests. Open it up, and you'll see two methods: authorize() and rules(). For most projects, you can return true in authorize() unless you're handling complex multi-tenant permissions. Inside rules(), you define your constraints:

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

Now, inject this class into your controller method. Laravel automatically executes these rules before your code even reaches the controller body. If validation fails, Laravel handles the redirect and error messages for you automatically.

Why Not Just Validate in the Controller?

We've all been tempted to use $request->validate() directly in the controller because it's fast. I’ve done it plenty of times. But it breaks the "Single Responsibility Principle." Your controller should handle the flow of the request, not the nitty-gritty of whether a password has enough special characters.

By using FormRequest classes, your controllers stay skinny. This makes it much easier to integrate with other parts of your app, like when you’re Designing a clean service layer in Laravel without over-abstraction to handle your business logic.

A Common Pitfall: Custom Error Messages

One thing that trips up juniors is the default error messages. They’re often generic, like "The email has already been taken." If you need to customize these, you don't need to write custom logic. Just add a messages() method to your FormRequest class:

PHP
public function messages(): array
{
    return [
        'email.unique' => 'We already have an account with that email address.',
        'password.min' => 'Your password is too short. Please use at least 8 characters.',
    ];
}

Frequently Asked Questions

Q: What if I need to validate data based on the user's role? A: Use the authorize() method in your FormRequest. You can access the authenticated user via $this->user() and return a boolean based on their permissions.

Q: Can I use FormRequests for API endpoints? A: Absolutely. If the request is an AJAX or API call, Laravel detects this and returns a JSON response with a 422 Unprocessable Entity status code automatically. No extra configuration is needed.

Q: Should I use FormRequests for every single input? A: Not necessarily. For simple search forms or one-off inputs, $request->validate() is fine. Save FormRequest classes for forms where you have complex logic or repeated usage.

Wrapping Up

Effective form validation in Laravel is about consistency. Once you stop writing manual checks, you'll find that your controllers are shorter, your bugs are easier to track, and your tests are cleaner. I still find myself occasionally taking the shortcut when I'm prototyping, only to refactor it into a FormRequest two hours later when the logic gets messy. Don't be afraid to pull that code out early—it saves you about an hour of headache later in the development cycle.

Back to Blog

Similar Posts

Notebook labeled 'Mistake' next to a red delete eraser on a dark background.
LaravelJune 20, 20264 min read

7 Laravel errors every beginner hits (and how to fix them)

7 Laravel errors every beginner hits? Don't panic. Learn how to fix common routing, Eloquent, and validation mistakes to speed up your development process.

Read more
Vintage wooden signpost in foggy mountain landscape, indicating hiking trails and altitudes.
Laravel
June 20, 2026
3 min read

Laravel routing and controllers: A Beginner's Guide to MVC

Master Laravel routing and controllers to build clean, maintainable web applications. Learn how the request lifecycle works and how to structure your code.

Read more
A vintage typewriter with a paper displaying 'Terms of Service'. Perfect for business or legal themes.
LaravelJune 20, 20264 min read

Structuring a Laravel package for long-term maintainability

Structuring a Laravel package correctly prevents technical debt. Learn how to organize your files, manage dependencies, and write code that lasts.

Read more