[Extension] yii2-fluent-rules: A type-safe and fluent validation interface for Yii2

Hi everyone!

I wanted to share a new open-source package I’ve been working on to solve a common pain point when building large-scale applications with Yii2: the lack of type-safety and IDE autocompletion in model validation rules.

As we all know, the traditional way of defining rules in Yii2 relies heavily on nested arrays and magic strings:

// The traditional way (prone to typos, no IDE support)
public function rules()
{
    return [
        [['email'], 'required'],
        [['email'], 'email'],
        [['status'], 'integer', 'min' => 1, 'max' => 10],
    ];
}

If you misspell ‘integer’ or type ‘mn’ instead of ‘min’, the IDE won’t warn you, and you’ll only find out at runtime.

To fix this, I created yii2-fluent-rules. It provides an object-oriented, fluent builder API that brings strict typing and full autocompletion to your model rules.

How it looks:

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

public function rules()
{
    return RuleBuilder::rules([
        Attribute::create('email')->notNull()->email(),
        Attribute::create('status')->integer()->min(1)->max(10)
    ]);
}

Key Features:

  • :shield: 100% Type-Safe: Every core Yii2 validator has its own dedicated builder with typed methods.
  • :brain: Full IDE Autocompletion: No more guessing property names or looking up the documentation for validator configurations.
  • :arrows_counterclockwise: Seamless Migration-to-Rule Mapping: The fluent API closely mirrors standard database column definitions, making it incredibly fast and intuitive to translate your database migrations into model rules.
  • :twisted_rightwards_arrows: Hybrid Pattern Support: You don’t have to go all-in. It seamlessly integrates with traditional array-based rules, allowing you to use the fluent builder for standard validations while keeping custom or legacy rules intact.
  • :rocket: Strict Quality Standards: Fully compatible with PHP 8.2+ and heavily tested under PHPStan (Level 5) to ensure architectural compliance.
  • :robot: Gii Integration: The package includes a built-in Gii Template for standard model generation. Additionally, it provides an automatic parsing utility (GiiHelper) intended for library extenders.

Installation

You can install it directly via Composer:

composer require ovargas/fluentrules

Links

I would love to hear your thoughts, feedback, or any suggestions for improvements!

Best regards,
Omar

Does extension supports GII integration? Adding it to GII model generation template will reduce the effort.

Hi!

Yes, absolutely. That was a great suggestion, and I actually had the exact same thought while refining the workflow.

I am happy to announce that Gii integration is now fully supported starting from version 1.0.0. The package now includes a built-in Gii Model Template and a dedicated GiiHelper utility class.

Here is a quick overview of how it works:

You can easily register the new template in your application configuration (main-local.php or web.php) by adding the blueprint path from the @vendor directory. Once configured, you can select the “Fluent Rules” layout directly from Gii’s Code Template dropdown menu when generating models.

I have completely updated the README.md file on GitHub with step-by-step instructions on how to enable the template in your development environment, alongside concrete code examples for both standard usage and custom template extensions.

Please update to the latest stable release via Composer, give it a spin, and let me know your thoughts. If you encounter any edge cases or have further feedback, feel free to open an issue on the repository!

Best regards.

1 Like