sandermuller/laravel-fluent-validation

Fluent validation rule builders for Laravel
47,149 220
Install
composer require sandermuller/laravel-fluent-validation
Latest Version:1.34.0
PHP:^8.2
License:MIT
Last Updated:Sep 16, 2026
Links: GitHub  ·  Packagist
Maintainer: SanderMuller

Laravel Fluent Validation

Fluent validation rule builders for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub PHPStan Action Status Total Downloads License Laravel Compatibility

Write Laravel validation rules with IDE autocompletion instead of memorizing string syntax. Each rule type exposes only the methods that apply to it: FluentRule::string() won't offer digits(), FluentRule::date() won't offer mimes(). each() and children() keep parent and child rules in one place instead of scattered across dot-notation keys. For large arrays, the HasFluentRules trait makes wildcard validation up to 160x faster.

// Before
'name'         => 'required|string|min:2|max:255',
'email'        => ['required', 'email', Rule::unique('users')->ignore($id)],
'role'         => Rule::when($isAdmin, 'required|string|in:admin,editor'),
'items'        => 'array',
'items.*.id'   => 'required|integer|exists:items,id',
'items.*.name' => 'required|string|max:255',

// After
'name'  => FluentRule::string('Full Name')->required()->min(2)->max(255),
'email' => FluentRule::email('Email')->required()->unique('users', 'email', fn ($r) => $r->ignore($id)),
'role'  => FluentRule::string()->when($isAdmin, fn ($r) => $r->required()->in(['admin', 'editor'])),
'items' => FluentRule::array()->each([
    'id'   => FluentRule::integer()->required()->exists('items', 'id'),
    'name' => FluentRule::string()->required()->max(255),
]),

Migrating an existing codebase? Jump straight to Migrating to fluent validation; a companion package automates the bulk of the rewrite.

Installation

You can install the package via composer:

composer require sandermuller/laravel-fluent-validation

Requires PHP 8.2+ and Laravel 12+. See Installation for AI-assisted development with Laravel Boost, and UPGRADING.md when upgrading from an older release.

Usage

Add the HasFluentRules trait to your form request:

use Illuminate\Foundation\Http\FormRequest;
use SanderMuller\FluentValidation\FluentRule;
use SanderMuller\FluentValidation\HasFluentRules;

class StorePostRequest extends FormRequest
{
    use HasFluentRules;

    public function rules(): array
    {
        return [
            'title'    => FluentRule::string('Title')->required()->min(2)->max(255),
            'email'    => FluentRule::email('Email')->required()->unique('users'),
            'date'     => FluentRule::date('Publish Date')->required()->afterToday(),
            'avatar'   => FluentRule::image()->nullable()->max('2mb'),
            'tags'     => FluentRule::array(label: 'Tags')->required()->each(
                              FluentRule::string()->max(50)
                          ),
            'password' => FluentRule::password()->required()->mixedCase()->numbers(),
        ];
    }
}

The label 'Title' replaces :attribute in error messages. You get "The Title field is required" instead of "The title field is required", without a separate attributes() array.

See Basic usage for the schema() builder, typing your rules() return, and using fluent rules outside form requests.

Documentation

Read the full documentation at sandermuller.github.io/laravel-fluent-validation.

Getting started

Digging deeper

  • Extending parent rules: child form requests, modifyEach, modifyChildren, returning a RuleSet
  • Livewire: HasFluentValidation trait, Filament workaround
  • Performance: O(n) wildcards, pre-evaluation, fast-check closures, batched DB
  • Benchmarks: the measured numbers and the rule sets behind them
  • RuleSet: build, compose, inspect, escape hatches, method reference
  • Validating with a RuleSet: validate(), check(), unknown fields, error bags
  • Testing: FluentRulesTester, Pest expectations
  • Rule reference: all types, modifiers, conditionals, macros

Migration and tooling

Contributing

Please see CONTRIBUTING for details.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

MIT License. Please see License File for more information.

Related Packages

kentjerone/laravel-chain-rule

A chainable rule builder for Laravel validation.

1,013 0
timacdonald/rule-builder

A fluent rule builder for Laravel validation rule generation.

8,178 101
prettus/laravel-validation

Laravel Validation Service

11,913,613 406
skysplit/laravel5-intl-translation

Laravel 5 package for better translation syntax using php-intl extension

106,829 10
pixelpeter/laravel5-isocodes-validation

Laravel 5 wrapper for the IsoCodes Validation library from ronanguilloux

87,867 19