PHPackages                             szunisoft/laravel-rule-groups - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. szunisoft/laravel-rule-groups

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

szunisoft/laravel-rule-groups
=============================

Make customizable and reusable rule groups easily..

1.0.11(7y ago)12.6k1[1 issues](https://github.com/SzuniSOFT/laravel-rule-groups/issues)[1 PRs](https://github.com/SzuniSOFT/laravel-rule-groups/pulls)MITPHPPHP ^7.1.3CI failing

Since Jun 28Pushed 6y ago1 watchersCompare

[ Source](https://github.com/SzuniSOFT/laravel-rule-groups)[ Packagist](https://packagist.org/packages/szunisoft/laravel-rule-groups)[ RSS](/packages/szunisoft-laravel-rule-groups/feed)WikiDiscussions 1.0 Synced 2mo ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

[![GitHub release](https://camo.githubusercontent.com/294e36175b5eef766336c82c79162c73431f933fc4829c2b0885646f6ff04243/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f537a756e69534f46542f6c61726176656c2d72756c652d67726f7570732e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/SzuniSOFT/laravel-rule-groups/releases)[![Packagist](https://camo.githubusercontent.com/f10d9df083cf4522afa6db97b02296aaba536f812e9abd2e144e88e44057320e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f737a756e69736f66742f6c61726176656c2d72756c652d67726f7570732e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/szunisoft/laravel-rule-groups)[![license](https://camo.githubusercontent.com/82d9b8d0ffbb67ff882037ca551aebf20e1fd412e8c37c4cf223131e2a8e4387/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f737a756e69736f66742f6c61726176656c2d72756c652d67726f7570732e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/SzuniSOFT/laravel-rule-groups)

Rule Groups
===========

[](#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
----------

[](#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
-------------

[](#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
--------------------

[](#creating-rule-groups)

```
php artisan make:rule-group CompanyRuleGroup

```

Writing Rule Groups
-------------------

[](#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
-------------------------

[](#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
----------------------------------------

[](#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

[](#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.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Total

4

Last Release

2799d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bea64d6c8d626db499bd4268c8e73a37889b3954ca0b9b4eaaa736cb63b800dd?d=identicon)[sykorax](/maintainers/sykorax)

---

Top Contributors

[![sykorax](https://avatars.githubusercontent.com/u/5334912?v=4)](https://github.com/sykorax "sykorax (21 commits)")

---

Tags

laravelreusabilitylaravelvalidationrulereusablegroup

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/szunisoft-laravel-rule-groups/health.svg)

```
[![Health](https://phpackages.com/badges/szunisoft-laravel-rule-groups/health.svg)](https://phpackages.com/packages/szunisoft-laravel-rule-groups)
```

###  Alternatives

[illuminatech/validation-composite

Allows uniting several validation rules into a single one for easy re-usage

184485.5k](/packages/illuminatech-validation-composite)[timacdonald/rule-builder

A fluent rule builder for Laravel validation rule generation.

1027.7k](/packages/timacdonald-rule-builder)[carsdotcom/laravel-json-schema

Json Schema validation for Laravel projects

1036.7k3](/packages/carsdotcom-laravel-json-schema)[fadion/rule

An expressive validation rule builder for Laravel.

131.1k](/packages/fadion-rule)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
