Laravel Fluent Validation: An Object-Oriented Rule Builder

Last updated on by

Laravel Fluent Validation: An Object-Oriented Rule Builder image

Laravel Fluent Validation, by Sander Muller, is a package that replaces Laravel's traditional string-based validation syntax with a fluent, object-oriented API. It provides IDE autocompletion and type safety, ensuring that you only apply rules that make sense for the data type being validated.

Fluent and Type-Safe Rules

Instead of concatenating strings like 'required|string|min:2|max:255', you can use a chain of methods. The API is context-aware, meaning that once you define a rule type (such as a string or a date), only the methods relevant to that type are available:

use SanderMuller\LaravelFluentValidation\FluentRule;
 
$rules = [
'name' => FluentRule::string()->required()->min(2)->max(255),
'published_at' => FluentRule::date()->after('today')->nullable(),
'age' => FluentRule::integer()->min(18)->max(99),
];

Nested Array Validation

The package simplifies validating nested data structures by using each() and children() methods. This allows you to define rules in a way that mirrors your data structure rather than using flat dot-notation keys:

'items' => FluentRule::array()->required()->min(1)->each(
FluentRule::children([
'id' => FluentRule::integer()->required()->exists('products', 'id'),
'quantity' => FluentRule::integer()->required()->min(1),
])
),

Performance Optimizations

Beyond the syntax improvements, the package includes several performance-focused features:

  • O(n) Wildcard Expansion: For large nested arrays, the package uses an optimized expansion algorithm that can be significantly faster than Laravel's native O(n²) approach.
  • Fast-Check Closures: Common rules are compiled into PHP closures, allowing the validator to bypass the overhead of the main validation engine for passing values.
  • Batched Database Queries: exists and unique rules for wildcard arrays are automatically batched into a single whereIn query to reduce database load.

Integrated Messages and Labels

You can define human-readable labels and custom error messages directly within the rule chain. This removes the need to maintain separate attributes() or messages() arrays in your Form Requests:

'email' => FluentRule::string()
->email()
->label('Email Address')
->message('Please provide a valid business email.', 'email'),

Installation

You can install the package via Composer:

composer require sandermuller/laravel-fluent-validation

The package requires PHP 8.2+ and Laravel 11.0 or higher. It also includes dedicated traits for integrating with Livewire and Filament projects.

You can learn more about this package and view the full documentation on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
LaraOwl: Self-Hosted Monitoring for Laravel Applications image

LaraOwl: Self-Hosted Monitoring for Laravel Applications

Read article
Filament Storage Monitor: Track Disk Usage From Your Filament Dashboard image

Filament Storage Monitor: Track Disk Usage From Your Filament Dashboard

Read article
Subscriptionify: Feature-Based Subscription Management for Laravel image

Subscriptionify: Feature-Based Subscription Management for Laravel

Read article
Toolkit: Reusable AI Tools for the Laravel AI SDK image

Toolkit: Reusable AI Tools for the Laravel AI SDK

Read article
Laracon US 2026 Reveals Its Full Speaker Lineup image

Laracon US 2026 Reveals Its Full Speaker Lineup

Read article
The State of PHP 2026 Survey Is Now Open image

The State of PHP 2026 Survey Is Now Open

Read article