sourcetoad/rule-helper-for-laravel
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 |
|---|---|
| 13.x | 6.3.x |
| 12.x | 6.x |
| 11.x | 5.x |
| 10.x | 4.x |
| 9.x | 3.x |
| 9.x | 2.x |
| 8.x | 1.x |
sourcetoad/rule-helper-for-laravel 适用场景与选型建议
sourcetoad/rule-helper-for-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 169.51k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sourcetoad/rule-helper-for-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sourcetoad/rule-helper-for-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 169.51k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 14
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-22