stankata90/yii2-rules-generator

Yii 框架的规则生成器

v1.0.0 2021-03-07 15:11 UTC

This package is auto-updated.

Last update: 2024-09-08 01:55:26 UTC


README

PHP from Packagist PHP from Packagist Latest Stable Version Total Downloads

为模型规则生成快速简单的数组,为验证器及其选项提供自动完成提示。

亮点

  • 简单安装
  • 简化事物

安装

使用 Composer 以常规方式安装。将以下内容添加到您的 composer.json 文件的要求部分

"stankata90/yii2-rules-generator": "*"

或运行

composer require stankata90/yii2-rules-generator

文档

示例标准模型文件

<?php

    namespace frontend\models;

    use Yii;
    use yii\base\Model;

    /**
     * ContactForm is the model behind the contact form.
     */
    class ContactForm extends Model
    {

        public $name;
        public $email;
        public $subject;
        public $body;
        public $verifyCode;


        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                // name, email, subject and body are required
                [ [ 'name', 'email', 'subject', 'body' ], 'required' ],
                // email has to be a valid email address
                [ 'email', 'email' ],
                // verifyCode needs to be entered correctly
                [ 'verifyCode', 'captcha' ],
            ];
        }
    }
?>

在模型中安装生成器

要访问生成器,您需要使用 RulesGenerator 特性

<?php

    namespace frontend\models;

    use stankata90\Yii2RulesGenerator\RulesGenerator;
    use Yii;
    use yii\base\Model;

    /**
     * ContactForm is the model behind the contact form.
     */
    class ContactForm extends Model
    {
        use RulesGenerator;
        
        public $name;
        public $email;
        public $subject;
        public $body;
        public $verifyCode;


        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                // name, email, subject and body are required
                [ [ 'name', 'email', 'subject', 'body' ], 'required' ],
                // email has to be a valid email address
                [ 'email', 'email' ],
                // verifyCode needs to be entered correctly
                [ 'verifyCode', 'captcha' ],
            ];
        }
    }
?>

使用生成器

使用特性后,在模型上下文中会出现另一个 "rulesGenerator" 方法。使用此方法,我们将生成用于 yii2 的规则数组。

<?php
    class ContactForm extends Model
    {
        use RulesGenerator;
        
        public $name;
        public $email;
        public $subject;
        public $body;
        public $verifyCode;

        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            $r = $this->rulesGenerator();

            $r->required([ 'name', 'email', 'subject', 'body' ]);
            $r->email('email');
            $r->string('name')->min(10)->max(100)->tooShort('To short message')->tooLong('To long message');
            $r->captcha('verifyCode');
            
            // using custom in rule
            $r->string('name_2')->custom('min', 10)->custom('max', 100)->custom('tooShort', 'To short message')->custom('tooLong', 'To long message');
    
            return $this->rulesGenerator()->getRules();
        }
    }
?>

创建模板。

您可以使用初始规则设置创建模板,这可以节省不必要的规则重复。要创建模板,无论文件在哪个文件夹中,都扩展 Custom_.php 文件。例如,"frontend\rules\StringRules.php"

<?php

    namespace frontend\rules;

    use stankata90\Yii2RulesGenerator\rules\Custom_;

    class StringRules extends Custom_
    {

        public function template_string10() {
            return $this->rulesGenerator()->string( $this->getAttributes() )->max(10);
        }

        public function template_string255() {
            return $this->rulesGenerator()->string( $this->getAttributes() )->max(255);
        }

        public function template_string100() {
           return $this->rulesGenerator()->string( $this->getAttributes() )->max(100);
        }

    }
?>

使用模板。

    public function rules()
    {
        $r = $this->rulesGenerator();

        $r->required([ 'name', 'email', 'subject', 'body' ]);
        $r->email('email');
        $r->string('name')->min(10)->max(100)->tooShort('To short message')->tooLong('To long message');
        $r->captcha('verifyCode');
        
        // using custom in rule
        $r->string('name_2')->custom('min', 10)->custom('max', 100)->custom('tooShort', 'To short message')->custom('tooLong', 'To long message');

        // use the string100 template and add more settings
        $customString = ( new StringRules('name' ) )->template_string100()->message('Custom message');
        
        // use the string255 template
        $customString255 = ( new StringRules('name' ) )->template_string255();

        // send $customString and $customString255 to rulesGenerator before get all the rules
        return $this->rulesGenerator( [ $customString, $customString255 ] )->getRules();
    }