Two Laravel devs that won't disappear on you. Finally! Hire Joel and Aaron from No Compromises.

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
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Lucky Media logo

Lucky Media

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

Lucky Media
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
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
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
Tinkerwell logo

Tinkerwell

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

Tinkerwell
PhpStorm logo

PhpStorm

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

PhpStorm
Shift logo

Shift

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

Shift
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
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

The latest

View all →
Manage Subscription Plans and Entitlements in Laravel with Laravel Entitlements image

Manage Subscription Plans and Entitlements in Laravel with Laravel Entitlements

Read article
Moat: A Security Review for Your GitHub Account image

Moat: A Security Review for Your GitHub Account

Read article
Laravel Paper: A Flat-File Eloquent Driver image

Laravel Paper: A Flat-File Eloquent Driver

Read article
Simple Feature Flags for Laravel with Laravel Toggle image

Simple Feature Flags for Laravel with Laravel Toggle

Read article
Manage Laravel Cloud Deployments Inside PhpStorm image

Manage Laravel Cloud Deployments Inside PhpStorm

Read article
Piper: Laravel-Style Array and String Helpers for PHP's Pipe Operator image

Piper: Laravel-Style Array and String Helpers for PHP's Pipe Operator

Read article