PHPackages                             fadion/rule - 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. fadion/rule

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

fadion/rule
===========

An expressive validation rule builder for Laravel.

1.1(10y ago)131.1k4[1 issues](https://github.com/fadion/Rule/issues)MITPHPPHP &gt;=5.4.0

Since Jan 25Pushed 10y ago1 watchersCompare

[ Source](https://github.com/fadion/Rule)[ Packagist](https://packagist.org/packages/fadion/rule)[ RSS](/packages/fadion-rule/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Rule
====

[](#rule)

A simple Laravel package that provides an expressive alternative to building validation rules. It uses what you already know about validation with Laravel and builds on top of that.

Instead of this:

```
$rules = [
    'username' => 'required|alpha',
    'email' => 'required|email'
];
```

You'll be writing:

```
Rule::add('username')->required()->alpha();
Rule::add('email')->required()->email();

$rules = Rule::get();
```

Which method is more easy to read or write is a matter of personal preference, so I'm not taking sides. However, using `Rule` offers two main advantages:

1. Misstyped rule names, non existing rules or missing arguments will throw fatal errors. In contrast, Laravel's rules will fail silently and provide no information when you write 'reqiured' instead of 'required'.
2. IDE suggestions and auto complete. Every rule is a real, documented method that your IDE can easily pick up. Coupled with [Laravel IDE Helper](https://github.com/barryvdh/laravel-ide-helper), you'll rarely need to open the Validator docs.

Installation
------------

[](#installation)

1. Add the package to your composer.json file and run `composer update`:

```
{
    "require": {
        "fadion/rule": "~1.1"
    }
}
```

2. Add `Fadion\Rule\RuleServiceProvider::class` to your `config/app.php` file, inside the `providers` array.
3. Add a new alias: `'Rule' => Fadion\Rule\Facades\Rule::class` to your `config/app.php` file, inside the `aliases` array.

> For Laravel 4, use version `1.0`

Usage
-----

[](#usage)

If you've used Laravel's Validator, then you already know how to use `Rule`. It just changes the way you write rules, but the validation process remains exactly the same.

**A complete validation example**

```
$inputs = Input::all();

Rule::add('username')->required()->alpha();
Rule::add('email')->required()->email();

$validator = Validator::make($inputs, Rule::get());

if ($validator->fails()) {
    Return::back()->withInput()->withErrors($validator);
}
```

**Rules with parameters**

Rule parameters are handled as simple method arguments. There can be one or more arguments, depending on the rule and some can be arrays too (like: `in`, `not_in`, `mimes`, etc.).

```
Rule::add('date')->date_format('mm/dd/YYYY');
Rule::add('age')->between(5, 15);
Rule::add('role')->in(['Admin', 'Moderator', 'Editor']);
```

**Exists and Unique**

`exists` and `unique` are special cases, as they have a few defined and documented arguments, as well as dynamic ones for the where clauses.

```
Rule::add('username')->exists('users');
Rule::add('username')->exists('users', 'name');
Rule::add('username')->exists('users', 'name', 'id', 10);

Rule::add('email')->unique('users');
Rule::add('email')->unique('users', 'email_address', 10);
Rule::add('email')->unique('users', 'email_address', 10, 'account_id', 1);
```

**Custom rules**

`Rule` handles custom rules in the same way as the predefined ones. It even accepts parameters.

```
Validator::extend('foo', function($attribute, $value, $parameters) {
    return $value == 'foo';
});

Rule::add('name')->required()->foo();
```

**Array rule**

Laravel's rule for validating an input as `array` is renamed to `is_array()`. The word "array" is reserved in PHP and can't be used as a method name, hence the rename.

```
Rule::add('languages')->is_array();
```

**Method names**

Methods can be written as either snake\_case (as Laravel accepts rule names) or as camelCase. Both cases behave exactly the same, so use whatever feels better.

So, the methods below are equivalent:

```
Rule::add('date')->date_format('Y-m-d');
Rule::add('date')->dateFormat('Y-m-d');

Rule::add('username')->alpha_dash();
Rule::add('username')->alphaDash();
```

Attribute names
---------------

[](#attribute-names)

Laravel has an option to alias input names with custom attributes, as a way to build better error messages. `Rule` provides an easy way to create them.

```
Rule::add('name', 'Your name')->required();
Rule::add('email', 'Your email')->required()->email();

$validator = Validator::make(Input::all(), Rule::get());

// Apply attributes
$validator->setAttributeNames(Rule::getAttributes());
```

Messages
--------

[](#messages)

There's no way that you start writing expressive rules, but keep messages as arrays. `Rule` will handle those too in a very elegant and easy to use way. Just add a `message()` method after the rule:

```
Rule::add('email')
    ->required()->message("Email shouldn't be empty.")
    ->email()->message("Email appears to be invalid.");

Rule::add('password')
    ->required()->message("Password shouldn't be empty.")
    ->between(5, 15)->message("Make that password more secure!");

$rules = Rule::get();
$messages = Rule::getMessages();
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.1% 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 ~137 days

Total

2

Last Release

3992d ago

### Community

Maintainers

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

---

Top Contributors

[![fadion](https://avatars.githubusercontent.com/u/374519?v=4)](https://github.com/fadion "fadion (34 commits)")[![andrioli](https://avatars.githubusercontent.com/u/499182?v=4)](https://github.com/andrioli "andrioli (1 commits)")

---

Tags

laravelvalidationrule

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fadion-rule/health.svg)

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

###  Alternatives

[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M107](/packages/propaganistas-laravel-phone)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[illuminatech/validation-composite

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

184485.5k](/packages/illuminatech-validation-composite)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)[galahad/laravel-addressing

Laravel package providing addressing functionality

70316.6k](/packages/galahad-laravel-addressing)

PHPackages © 2026

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