sourcetoad/rule-helper-for-laravel 问题修复 & 功能扩展

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

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

sourcetoad/rule-helper-for-laravel

最新稳定版本:v6.2.0

Composer 安装命令:

composer require sourcetoad/rule-helper-for-laravel

包简介

Rule helper for Laravel

README 文档

README

Adds helpers to make building Laravel rule arrays easier by providing helper methods for the built-in rules.

Installation

composer require sourcetoad/rule-helper-for-laravel

Usage

RuleSet

The RuleSet class provides a fluent interface for defining sets of rules.

Basic usage

use App\Rules\CustomRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Unique;
use Sourcetoad\RuleHelper\RuleSet;

class CreateBlogRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => RuleSet::create()
                ->required()
                ->unique('posts', 'title', fn(Unique $rule) => $rule->withoutTrashed())
                ->rule(new CustomRule)
                ->max(255),
            'body' => RuleSet::create()
                ->required(),
        ];
    }
}

Rule

The Rule class provides the same methods as the Laravel \Illuminate\Validation\Rule class, with the rest of the built-in rules exposed via static methods.

Basic usage

use Illuminate\Foundation\Http\FormRequest;
use Sourcetoad\RuleHelper\Rule;

class CreateBlogRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => [
                Rule::required(),
                Rule::unique('posts'),
                Rule::max(255),
            ],
            'body' => [
                Rule::required(),
            ],
        ];
    }
}

Additional helpers

Defined rule sets

The RuleSet class contains methods to define and reuse rule sets across the project.

To define a rule set call RuleSet::define in your app service provider's boot method.

    public function boot(): void
    {
        RuleSet::define('existing_email', RuleSet::create()->email()->exists('users'));
    }

The defined set can then be used in rules using RuleSet::useDefined.

    public function rules(): array
    {
        return [
            'to' => RuleSet::useDefined('existing_email')->required(),
            'bcc' => RuleSet::useDefined('existing_email'),
        ];
    }

To concatenate a defined rule set you can call concatDefined with the rule set's name.

RuleSet::create()->required()->concatDefined('existing_email');

requiredIfAll

Accepts multiple RequiredIf rules and only marks as required if all return true.

requiredIfAny

Accepts multiple RequiredIf rules and marks as required if any return true.

Version Compatibility

Laravel Rule Helper
12.x 6.x
11.x 5.x
10.x 4.x
9.x 3.x
9.x 2.x
8.x 1.x

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 7
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固