定制 szunisoft/laravel-rule-groups 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

szunisoft/laravel-rule-groups

Composer 安装命令:

composer require szunisoft/laravel-rule-groups

包简介

Make customizable and reusable rule groups easily..

README 文档

README

GitHub release Packagist license

Rule Groups

With Laravel it's very easy to run predefined validation rules and customize them. It is also possible to create your own rule in several ways.

One of our projects uses tons of validations and most of them are the same or very similar to each other. This package provides an easy way to make reusable validation rule groups.

The primary advantage of this package is the code reusability with centralized rule group controlling and managing.

Installing

composer require szunisoft/laravel-rule-groups

If you are on a lower version of Laravel and you don't have package discovery yet please add the ServiceProvider to the config/app.php configuration file.

'providers' => [
    ...
    
    /*
     * Package Service Providers...
     */
     \SzuniSoft\RuleGroups\Providers\RuleGroupServiceProvider::class
]

Configuration

By default the package will generate all rule groups into the app/RuleGroups directory. You can change it by publishing the configuration file.

php artisan vendor:publish --provider="\SzuniSoft\RuleGroups\Providers\RuleGroupServiceProvider" --tag="config"

Creating Rule Groups

php artisan make:rule-group CompanyRuleGroup

Writing Rule Groups

Locate the generated class. Default location is app/RuleGroups.

use SzuniSoft\RuleGroups\RuleGroup;

class CompanyRuleGroup extends RuleGroup {

    protected function getAttributeRules() {
        
        // Here we go..
        
        return [
        
            // This is self explained
            'name' => ['required'],
            
            // You don't have to take care of when use or not to use inline formats
            'vat_number' => 'required|min:5',
            
            // You can use your custom validation rules 
            // just like you normally would.
            'phone' => new MyVeryCustomAndFavoritePhoneRule(),
            
            // Laravel built in Rule is welcomed too
            'country' => ['required', Rule::exists('countries', 'iso_2')],
        ];
    }

}

Basic Usage - Reusability

You can easily use rule groups in your controllers. See the example.

public function register(Request $request) {
    
        $this->validate($request, CompanyRuleGroup::rules());
        
        // Further secret business logic..
    
    }

This will be equivalent with the following:

public function register(Request $request) {
    
        $this->validate($request, [
            'name' => ['required'],
            'vat_number' => ['required', 'min:5'],
            'phone' => new MyVeryCustomAndFavoritePhoneRule(),
            'country' => ['required', Rule::exists('countries', 'iso_2')],
        ]);
        
        // Further secret business logic..
    
    }

Now you can use this validation group in any other controllers or wherever you want to validate.

Advanced Usage - On demand configuration

In this chapter we'll take a closer look at how we can modify these groups in extremist situations.

Let's say we have a registration page where the user must specify the information of the managed company but we also need billing information. That's okay, easy and simple. But what if the managed company and the billing company are not the same one?

Take a look at the following Rule Group

use SzuniSoft\RuleGroups\RuleGroup;

class CompanyRuleGroup extends RuleGroup {

    protected function getAttributeRules() {
        
        return [
            'name' => ['required'],
            'vat_number' => ['required', 'min:5'],
            'phone' => new MyVeryCustomAndFavoritePhoneRule(),
            'country' => ['required', Rule::exists('countries', 'iso_2')],
            'state' => ['required', 'max:50'],
            'city' => ['required', 'max:50'],
            'zip_code' => ['required', 'max:50'],
            'address' => ['required', 'max:250']
        ];
    }

}

Now let's use it in our controller

public function register (Request $request) {

    $this->validate($request, array_merge(
        CompanyRuleGroup::attributes()->prefixed('managed')->toArray(),
        CompanyRuleGroup::attributes()->prefixed('billed')->toArray(),
    ));

}

This will turn into this

public function register (Request $request) {

    $this->validate($request, [
        
        'managed.name' => ['required'],
        'managed.vat_number' => ['required' ,'min:5'],
        'managed.phone' => new MyVeryCustomAndFavoritePhoneRule(),
        'managed.country' => ['required', Rule::exists('countries', 'iso_2')],
        'managed.state' => ['required', 'max:50'],
        'managed.city' => ['required', 'max:50'],
        'managed.zip_code' => ['required', 'max:50'],
        'managed.address' => ['required', 'max:250'],
        
        'billed.name' => ['required'],
        'billed.vat_number' => ['required', 'min:5'],
        'billed.phone' => new MyVeryCustomAndFavoritePhoneRule(),
        'billed.country' => ['required', Rule::exists('countries', 'iso_2')],
        'billed.state' => ['required', 'max:50'],
        'billed.city' => ['required', 'max:50'],
        'billed.zip_code' => ['required', 'max:50'],
        'billed.address' => ['required', 'max:250'],
    
    ]);

}

Let's say it's not enough for us and we need almost the same rules but not exactly.

We want to validate the billed company inputs only when the user want to specify different one.

public function register (Request $request) {

    $this->validate($request, array_merge(
        CompanyRuleGroup::attributes()->prefixed('managed')->toArray(),
        CompanyRuleGroup::attributes()
        
            // Adds rule(s) to all attribute in the group
            ->addToAll('required_without:user_wants_managed_as_billed')
            
            ->prefixed('billed')
            ->toArray(),
    ));

}

Available methods

After using the attributes() static method you can use the following ones:

Attribute management

  • addRulesTo($attribute, $rules) Adds new rules to the attribute. Attribute will be created if not exists.
  • overwriteRulesOf($attribute, $rules) Overwrites rules of a specific attribute.
  • without($attribute) Removes an attribute and it's rules from the group.
  • forgetRulesOf($attribute) Removes all rules from an attribute.
  • addToAll($rules) Apply the given rules on all attributes.

Utility

  • restore() Restores the group to it's initial state.
  • prefix($prefix) Applies prefix on all attributes. You can use deep dotting array access pattern (x.y.z)

Don't forget to invoke the toArray() method.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-06-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固