PHP code example of ovargas / fluentrules

1. Go to this page and download the library: Download ovargas/fluentrules library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ovargas / fluentrules example snippets


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

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']),
    ]);
}

$this->createTable('{{%location}}', [
    'id' => $this->primaryKey(),
    'name' => $this->string()->notNull(),
    'age' => $this->integer()->append('CHECK (age >= 18 AND age <= 99)'),
    'status' => $this->string()->defaultValue('active')->check("status IN ('active', 'inactive', 'deleted')"),
]);

return RuleBuilder::rules([
    Attribute::create('name')->string()->notNull(),
    Attribute::create('age')->integer()->min(18)->max(99),
    Attribute::create('status')->string()->defaultValue('active')->in(['active', 'inactive', 'deleted']),
]);

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

public function rules()
{
    return RuleBuilder::rules([
        // Fluent implementation
        Attribute::create('email')->email()->notNull()->unique(),
        
        // Native Yii2 standalone array rules (Supported seamlessly)
        ['password', 'validatePassword'],
        [['created_at', 'updated_at'], 'safe'],
    ]);
}

if (YII_ENV_DEV) {
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'generators' => [
            'model' => [
                'class' => 'yii\gii\generators\model\Generator',
                'templates' => [
                    'Fluent Rules' => '@vendor/ovargas/fluentrules/templates/gii/model',
                ]
            ]
        ],
    ];
}


// Further down, print it into your custom rules() skeleton template layout:
public function rules()
{
    return RuleBuilder::rules([
         echo INDENT . INDENT . implode(',' . PHP_EOL . INDENT . INDENT, $fluentRules) . PHP_EOL;