sourcetoad/rule-helper-for-laravel

Rule helper for Laravel
205,230 9
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
Maintainer: sourcetoad

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

gabrieloliverio/laravel5-generators

Database metadata-based generators for Laravel 5

1
erirk/paypalpayment

laravel-paypalpayment is simple package help you process direct credit card paym...

0
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

631,580,251 9,707
laravel/framework

The Laravel Framework.

580,311,802 34,925
laravel/tinker

Powerful REPL for the Laravel framework.

489,754,436 7,444