sourcetoad/rule-helper-for-laravel
| Install | |
|---|---|
composer require sourcetoad/rule-helper-for-laravel |
|
| Latest Version: | v7.0.1 |
| PHP: | ^8.3||^8.4||^8.5 |
| License: | MIT |
| Last Updated: | Sep 14, 2026 |
| Links: | GitHub · Packagist |
Rule Helper for Laravel
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 | 7.x |
| 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 |
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v7.0.1 | ^8.3| | ^13.24 | MIT | |
| v7.0.0 | ^8.3| | ^13.24 | MIT | |
| v6.4.0 | ^8.2| | ^12.44| | MIT | |
| v6.3.0 | ^8.2| | ^12.21| | MIT | |
| v6.2.0 | ^8.2| | ^12.21 | MIT | |
| v6.1.0 | ^8.2| | ^12.16 | MIT | |
| v6.0.0 | ^8.2| | ^12.0 | MIT | |
| v5.6.0 | ^8.2| | ^11.43 | MIT | |
| v5.5.0 | ^8.2| | ^11.23.2 | MIT | |
| v5.4.0 | ^8.2 | ^11.23.2 | MIT | |
| v5.3.1 | ^8.2 | ^11.23.2 | MIT | |
| v5.3.0 | ^8.2 | ^11.23.2 | MIT | |
| v5.2.0 | ^8.2 | ^11.8 | MIT | |
| v5.1.0 | ^8.2 | ^11.5 | MIT | |
| v5.0.0 | ^8.2 | ^11.0.3 | MIT | |
| v4.3.0 | ^8.1 | ^10.46 | MIT | |
| v4.2.2 | ^8.1 | ^10.34 | MIT | |
| v4.2.1 | ^8.1 | ^10.34 | MIT | |
| v4.2.0 | ^8.1 | ^10.34 | MIT | |
| v4.1.0 | ^8.1 | ^10.33 | MIT | |
| v4.0.0 | ^8.1 | ^10.14 | MIT | |
| v3.2.1 | ^8.0 | ^9.50.2| | MIT | |
| v3.2.0 | ^8.0 | ^9.50.2| | MIT | |
| v3.1.0 | ^8.0 | ^9.50.2| | MIT | |
| v3.0.0 | ^8.0 | ^9.45 | MIT | |
| v2.0.0 | ^8.0 | ^9.6 | MIT | |
| v1.0.0 | ^7.4| | ^8.82 | MIT |