PHPackages                             ovargas/fluentrules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. ovargas/fluentrules

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

ovargas/fluentrules
===================

An expressive, type-safe fluent validation rules interface for Yii2 Framework.

v1.0.0(2mo ago)15proprietaryPHPPHP &gt;=8.2.0

Since May 21Pushed 2mo agoCompare

[ Source](https://github.com/ovargasdev/yii2-fluent-rules)[ Packagist](https://packagist.org/packages/ovargas/fluentrules)[ RSS](/packages/ovargas-fluentrules/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

Yii2 Fluent Rules
=================

[](#yii2-fluent-rules)

[![Latest Stable Version](https://camo.githubusercontent.com/34e695c6016bc2a934a96bed696e29b2f2ab562a7134d65a55d00653cd506bea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d312e302e302d626c75652e737667)](https://packagist.org/packages/ovargas/fluentrules)[![Software License](https://camo.githubusercontent.com/4327260da2d567d3ebfe8d0b5063eb7ea391a01fc89807e3274d3982751692d8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d437573746f6d5f436f6d6d65726369616c2d6f72616e67652e737667)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/5814289f8aa8cd4671b9582f7c4172d2d1af7fccd603b5d707234f0e02ad69d8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e322d3838393262662e737667)](https://php.net)[![PHPStan](https://camo.githubusercontent.com/71e26d75f4bb037c8a8c23fe3b9c7ced7c4717969b5cbbb4dfce6cc09e63a99e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230352d737563636573732e737667)](https://phpstan.org/)

A lightweight, expressive, and type-safe fluent validation interface for the **Yii2 Framework**.

Tired of maintaining massive, multi-dimensional array trees in your ActiveRecord models? This package replaces arbitrary array configurations with an elegant, IDE-discoverable object chain, dramatically improving code readability, refactoring safety, and static analysis precision.

---

Key Features
------------

[](#key-features)

- 🌟 **100% Fluent API:** Chain your validation rules with full autocompletion and type safety.
- 🔀 **Hybrid Validation Engine:** Mix standard Yii2 array layouts alongside fluent definitions within the same compiler context.
- 🏢 **Mirror Architecture:** Features a specialized `RuleBuilder` class for every standard Yii2 core validator, plus convenience builders for streamlined workflows.
- 🛠️ **Migration-to-Model Workflow:** Matches Yii2's schema migration syntax, allowing you to copy, paste, and quickly refactor definitions using multi-cursors.
- 🛡️ **PHP 8.2 Strict Type-Safety:** Fully compatible with PHPStan and modern static analyzers.

---

Installation
------------

[](#installation)

Install the package via [Composer](https://getcomposer.org/):

```
composer require ovargas/fluentrules
```

---

Why Use Yii2 Fluent Rules?
--------------------------

[](#why-use-yii2-fluent-rules)

### The Traditional Yii2 Approach

[](#the-traditional-yii2-approach)

Standard validation rules in Yii2 depend heavily on raw nested arrays. They lack autocomplete support, are prone to typo errors, and quickly become hard to read:

```
public function rules()
{
    return [
        [['code', 'name'], 'required'],
        ['code', 'string', 'max' => 2],
        [['name', 'status'], 'string'],
        ['code', 'unique'],
        ['population', 'integer', 'skipOnEmpty' => false],
        [['status'], 'in', 'range' => ['active', 'inactive', 'deleted']],
        ['status', 'default', 'value' => 'active'],
    ];
}
```

### The Fluent Rules Approach

[](#the-fluent-rules-approach)

With this library, your logic remains clear, readable, and perfectly discoverable by any modern IDE:

```
use ovargas\fluentrules\Attribute;
use ovargas\fluentrules\RuleBuilder;

public function rules()
{
    return RuleBuilder::rules([
        Attribute::create('code')->string(2)->notNull()->unique(),
        Attribute::create('name')->string()->notNull(),
        Attribute::create('population')->integer()->skipOnEmpty(false),
        Attribute::create('status')->string()->defaultValue('active')->in(['active', 'inactive', 'deleted']),
    ]);
}
```

---

From Migration to Model in Seconds (Productivity Trick)
-------------------------------------------------------

[](#from-migration-to-model-in-seconds-productivity-trick)

Because the fluent interface mirrors the fluid syntax used by Yii2's Schema Builder during database migrations, writing your model constraints becomes incredibly fast. You can literally copy your column definitions from a migration file, paste them into your model's `rules()` method, and apply a few multi-cursor edits in your IDE:

### 1. Your Migration File

[](#1-your-migration-file)

```
$this->createTable('{{%location}}', [
    'id' => $this->primaryKey(),
    'name' => $this->string()->notNull(),
    'age' => $this->integer()->append('CHECK (age >= 18 AND age  'yii\gii\Module',
        'generators' => [
            'model' => [
                'class' => 'yii\gii\generators\model\Generator',
                'templates' => [
                    'Fluent Rules' => '@vendor/ovargas/fluentrules/templates/gii/model',
                ]
            ]
        ],
    ];
}
```

### 2. Generate Your Fluent Models

[](#2-generate-your-fluent-models)

2.1. Open Gii in your browser (/index.php?r=gii).

2.2. Go to the Model Generator.

2.3. Fill in your table details as usual.

2.4. Under the Code Template dropdown selection, choose "Fluent Rules".

2.5. Click Preview and then Generate.

### 3. Creating Custom Extensions (For Library Extenders)

[](#3-creating-custom-extensions-for-library-extenders)

If you or a third-party developer need to build a highly customized Gii template based on this package, you don't need to duplicate complex string-parsing algorithms. The GiiHelper::parseGiiRules() static method is globally available out of the box to process the core array definitions into clean fluent chains:

```
// Further down, print it into your custom rules() skeleton template layout:
public function rules()
{
    return RuleBuilder::rules([

    ]);
}
```

### Gii Generation Notice &amp; Feedback

[](#gii-generation-notice--feedback)

> **Note:** Since Gii code generation relies on strict string parsing of native array structures, highly complex rule definitions or uncommon core validators might occasionally render unexpected results.
>
> If you encounter any bugs, edge cases, or incorrectly formatted fluent outputs, please do not hesitate to **[open an issue on our GitHub repository](https://github.com/ovargas/yii2-fluent-rules/issues)**. Your feedback helps improve the parsing engine for the entire community!

---

License
-------

[](#license)

This package is licensed under a **Proprietary Hybrid License** by Omar Vargas.

- **Attribution:** Every use or fork must preserve copyright notices and credit the author.
- **Non-Commercial / Indirect Use:** Free to use for personal projects or internal business tools.
- **Direct Commercial Use (SaaS, Sales, Subscriptions):** Requires an explicit commercial license or royalty agreement per project.

For commercial licensing inquiries, please contact the author directly.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance87

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

64d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/179655343?v=4)[ovargasdev](/maintainers/ovargasdev)[@ovargasdev](https://github.com/ovargasdev)

---

Top Contributors

[![ovargasdev](https://avatars.githubusercontent.com/u/179655343?v=4)](https://github.com/ovargasdev "ovargasdev (13 commits)")

---

Tags

validatorvalidationfluentyii2rulestype-safe

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ovargas-fluentrules/health.svg)

```
[![Health](https://phpackages.com/badges/ovargas-fluentrules/health.svg)](https://phpackages.com/packages/ovargas-fluentrules)
```

###  Alternatives

[codeonyii/yii2-at-least-validator

Validates at least one (or more) attributes.

28263.7k1](/packages/codeonyii-yii2-at-least-validator)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13725.8k63](/packages/skeeks-cms)[iutrace/laravel-cuit-validator

Argentinian CUIT and CUIL Validator

1113.4k](/packages/iutrace-laravel-cuit-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
