stankata90/yii2-rules-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stankata90/yii2-rules-generator

Composer 安装命令:

composer require stankata90/yii2-rules-generator

包简介

The Rules Generator for the Yii framework

README 文档

README

PHP from Packagist PHP from Packagist Latest Stable Version Total Downloads

Generate quick and easy arrays for model rules, auto-complete tips for validators and their options.

Highlights

  • Simple installation
  • Simplify things

Installation

Install in the usual way with Composer. Add the following to the require section of your composer.json file:

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

or run

composer require stankata90/yii2-rules-generator

Documentation

Example standard model file:

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

Install the Generator in model

To access the generator, you need to use the trait 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' ],
            ];
        }
    }
?>

Using the generator

After using trait, another "rulesGenerator" method is presented in the context of the model. Using this method we will generate arrays with rules for 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();
        }
    }
?>

Create a Template.

You can create templates with initial rule settings, this would save unnecessary repetition of rules. To create a template, extend the Custom_.php file no matter which folder it is in. For example, "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);
        }

    }
?>

Using a Template.

    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();
    }

统计信息

  • 总下载量: 157
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2021-03-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固