Menü schliessen
Created: August 22nd 2025
Last updated: August 29th 2025
Categories: IT Development,  Laravel
Author: Ian Walser

Clean & Error-Free Form Validation In Laravel

Introduction

One of the most important aspects of building web applications is ensuring that user input is clean, safe, and valid. In Laravel, form validation is both powerful and beginner-friendly. This tutorial will guide you through the basics of Laravel validation and move into advanced techniques that help you write cleaner and more maintainable forms.

Why Validation Matters in Laravel

Without validation, your forms can quickly become a gateway for bugs, broken layouts, and even security issues. Laravel provides a wide range of built-in validation rules and an elegant syntax, so you can focus more on building features and less on debugging messy form submissions.

Basic Validation Using "validate()"

When handling forms in a controller, you can call the "validate()" method directly. For example, validating a simple registration form:

public function store(Request $request)
{
    $validated = $request->validate([
        'name'  => 'required|string|max:50',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|min:8|confirmed',
    ]);

    // If validation passes, proceed with creating the user
    User::create($validated);
}

This example checks that:

  • name is required, must be a string, and cannot exceed 50 characters.
  • email is required, must be a valid email, and unique in the "users" table.
  • password is required, at least 8 characters long, and confirmed with a matching "password_confirmation" field.

Validation in Form Requests

For cleaner controllers, Laravel allows moving validation rules into custom Form Request classes. This is useful as your rules grow more complex.

php artisan make:request StoreUserRequest
// In StoreUserRequest.php
public function rules()
{
    return [
        'name'  => 'required|string|max:50',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|min:8|confirmed',
    ];
}
// In your Controller
public function store(StoreUserRequest $request)
{
    User::create($request->validated());
}

This way, the controller remains clean, and validation logic is reusable.

Advanced Validation Techniques

1. Conditional Validation

Sometimes rules only apply under certain conditions. You can use the "sometimes()" method:

$request->validate([
    'company' => 'nullable|string',
    'company_id' => 'required_with:company|integer',
]);

Here, "company_id" is only required if a "company" value is present.

2. Custom Validation Rules

If built-in rules are not enough, you can create custom validation logic.

php artisan make:rule Uppercase
// In Uppercase.php
public function passes($attribute, $value)
{
    return strtoupper($value) === $value;
}

public function message()
{
    return 'The :attribute must be uppercase.';
}
// In controller or request
$request->validate([
    'code' => ['required', new Uppercase],
]);

3. Validating Arrays and Nested Data

Laravel makes it easy to validate array inputs, such as multiple addresses or tags:

$request->validate([
    'tags'   => 'required|array|min:1',
    'tags.*' => 'string|max:30',
]);

This ensures "tags" is an array with at least one item, and each item is a string of max 30 characters.

Best Practices for Cleaner Forms

  • If rules get very complex, seperate them into Form Request classes for better organization.
  • Leverage Laravel’s "Rule" class for complex conditions.
  • Use custom rules for domain-specific logic instead of stuffing controllers with checks.
  • Always validate nested arrays when handling JSON payloads or bulk form submissions.

Conclusion

Mastering validation in Laravel helps you prevent bad data, improve security, and build cleaner forms. By starting with the basics and moving into advanced features like custom rules and array validation, you’ll quickly develop forms that are both user-friendly and robust.