PHPackages                             franzose/kontrolio - 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. franzose/kontrolio

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

franzose/kontrolio
==================

Simple and standalone data validation library inspired by Laravel and Symfony

v2.0.2(1y ago)519035[2 issues](https://github.com/franzose/kontrolio/issues)MITPHPPHP &gt;=8.1CI failing

Since Jul 26Pushed 1y ago3 watchersCompare

[ Source](https://github.com/franzose/kontrolio)[ Packagist](https://packagist.org/packages/franzose/kontrolio)[ RSS](/packages/franzose-kontrolio/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (24)Used By (0)

[![](https://camo.githubusercontent.com/4698894d67a487ccb3d7fd9a85684322ac0e07e728cc185e2d91e89e0d793aa7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6672616e7a6f73652f6b6f6e74726f6c696f2e737667)](https://packagist.org/packages/franzose/kontrolio)[![CI](https://github.com/franzose/kontrolio/actions/workflows/ci.yaml/badge.svg)](https://github.com/franzose/kontrolio/actions/workflows/ci.yaml/badge.svg)

[На русском](https://github.com/franzose/kontrolio/blob/master/readme_rus.md)

Kontrolio: keep your data under control.
========================================

[](#kontrolio-keep-your-data-under-control)

Kontrolio is a simple standalone data validation library inspired by Laravel and Symfony and compatible with PHP 8.1+. \[ [Read on Medium](https://medium.com/@franzose/keep-your-data-under-control-530c23e59fb3) \]

Setting up validator
--------------------

[](#setting-up-validator)

The best way to set up validator:

```
// In container unaware environments
$valid = Factory::getInstance()->make($data, $rules, $messages)->validate();

// Using a service container implementation
$container->singleton('validation', static fn() => new Factory());
$container->get('validation')->make($data, $rules, $messages)->validate();
```

Of course, you can use `Kontrolio\Validator` class directly but then you'll need to provide available validation rules by yourself:

```
$validator = new Validator($data, $rules, $messages)->extend($custom)->validate();
```

Data here is supposed to be the key-value pairs—attributes and their values:

```
$data = [
    'foo' => 'bar',
    'bar' => 'baz',
    'baz' => 'taz'
];
```

Validation rules can be set in three different formats:

1. Laravel-like string
2. Instances of the class based rules
3. Callables (closures or callbacks)

You can also mix instances and callables when you set multiple validation rules to a single attribute. Here's a simple example:

```
$rules = [
    'one' => 'not_empty|length:5,15',
    'two' => new Email,
    'three' => static fn ($value) => $value === 'taz',
    'four' => [
        static fn ($value) => is_numeric($value),
        new GreaterThan(5),
    ]
];
```

When you set validation rules as string, validator will parse it to an ordinary array of rules before applying them to the attribute, so when you write `'some' => 'not_empty|length:5,15'`, it becomes

```
'some' => [
    new NotEmpty,
    new Length(5, 15)
]
```

It's pretty straightforward but remember that all arguments you pass after semicolon (separating them by commas) become the arguments of the validation rule constructor.

When you set validation rule as callback, internally it is wrapped by special object called `Kontrolio\Rules\CallbackRuleWrapper` to keep consistency defined by `Kontrolio\Rules\RuleInterface` interface.

Rule options
------------

[](#rule-options)

### Allowing empty value

[](#allowing-empty-value)

A single rule validation can be skipped when the validated attribute value is empty. If you feel that you need this option, you instantiate a rule using named constructor `allowingEmptyValue()` or by calling `allowEmptyValue()` method on the already existing instance:

```
'some' => [
    MyRule::allowingEmptyValue(),
    // (new MyRule())->allowEmptyValue()
]
```

### Skipping rule validation

[](#skipping-rule-validation)

When creating a new custom class based rule you might need an option to skip its validation based on some conditions. You can define the behavior using `canSkipValidation()` method:

```
class MyRule extends AbstractRule
{
    public function canSkipValidation($input = null)
    {
        return $input === 'bar';
    }

    // ...
}
```

Callable rules
--------------

[](#callable-rules)

Callable rule is nothing more than a closure or function that takes an attribute value and returns either boolean result of the validation or an options array equivalent to options provided by a class based validation rule:

```
    'foo' => static fn ($value) => is_string($value),
    'bar' => static function ($value) {
        return [
            // required when array
            'valid' => $value === 'taz',

            // optionals
            'name' => 'baz', // rule identifier
            'empty_allowed' => true, // allowing empty value
            'skip' => false // don't allow skipping current rule validation,
            'violations' => [] // rule violations
        ];
    }
```

Custom rules
------------

[](#custom-rules)

Of course you can create your custom rules. Just remember that each rule must be an instance of `Kontrolio\Rules\RuleInterface`, implement `isValid()` method and have an identifier. By default, identifiers are resolved by `Kontrolio\Rules\AbstractRule` and are based on the rule class name without namespace. However, you can override this behavior if you wish overriding `getName()` method.

Custom rules can be added eighter via factory or validator itself:

```
$factory = (new Factory())->extend([CustomRule::class]);

// with a custom identifier
$factory = (new Factory())->extend(['some_custom' => CustomRule::class]);
$validator = $factory->make([], [], []);

// if you don't use factory
$validator = new Validator([], [], []);
$validator->extend([CustomRule::class]);

// with a custom identifier
$validator->extend(['custom' => CustomRule::class]);
$validator->validate();
```

Bypassing attribute's value validation completely
-------------------------------------------------

[](#bypassing-attributes-value-validation-completely)

It's not the same as using `allowEmptyValue()` or `canSkipValidation()` on a rule. With those you can skip *only a rule*. But you can also bypass a whole attribute by using `Kontrolio\Rules\Core\Sometimes` rule. `Sometimes` tells validator to bypass validation when the value of the attribute is null or empty. That's all. You can prepend `Sometimes` to the attribute's rules array or use its identifier in a ruleset string:

```
$rules = [
    'one' => 'sometimes|length:5,15',
    // 'one' => [
    //     new Sometimes(),
    //     new Length(5, 15)
    // ]
];
```

Stopping validation on the first failure
----------------------------------------

[](#stopping-validation-on-the-first-failure)

You can tell the validator to stop validation if any validation error occurs:

```
$validator->shouldStopOnFailure()->validate();
```

Stopping certain attribute's validation on the first failure
------------------------------------------------------------

[](#stopping-certain-attributes-validation-on-the-first-failure)

Using `UntilFirstFailure` validation rule, you can also stop validation of a single attribute while keeping validation as a whole:

```
$data = [
    'attr' => '',
    'attr2' => 'value2'
];

$rules = [
    'attr' => [
        new UntilFirstFailure(),
        new NotBlank(),
        new NotFooBar()
    ]
];

$messages = [];

$validator = new Validator($data, $rules, $messages)->validate();
```

Now when `attr` fail with `NotBlank` rule, its validation will be stopped and validator will proceed to the `attr2`.

Error messages and rule violations
----------------------------------

[](#error-messages-and-rule-violations)

Error messages has a single simple format you'll love:

```
$messages = [
    'foo' => 'Foo cannot be null',
    'foo.length' => 'Wrong length of foo',
    'foo.length.min' => 'Foo is less than 3'
    // '[attribute].[rule].[violation]
];
```

Every message key can have three segments separated by dot. They are:

1. Attribute name
2. Validation rule identifier
3. Validation rule *violation*

With these options you can customize messages from the most general to the most specific. Each violation is set by the rule validating the attribute value. So when writing your own validation rule you may provide your own violations to provide customizability of the validation result and error messages.

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance46

Moderate activity, may be stable

Popularity27

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 97.6% 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 ~168 days

Recently: every ~90 days

Total

20

Last Release

395d ago

Major Versions

v1.4.7 → v2.0.02022-06-12

v1.5.1 → v2.0.12025-04-19

### Community

Maintainers

![](https://www.gravatar.com/avatar/98ef926a42fc68b2c0a8bc4865a3acda3a59d96e1a9be35c8585e5b9451d171e?d=identicon)[franzose](/maintainers/franzose)

---

Top Contributors

[![franzose](https://avatars.githubusercontent.com/u/708158?v=4)](https://github.com/franzose "franzose (82 commits)")[![maximkou](https://avatars.githubusercontent.com/u/2118695?v=4)](https://github.com/maximkou "maximkou (2 commits)")

---

Tags

laravelphpsymfonyvalidationvalidatorsymfonylaravelvalidation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/franzose-kontrolio/health.svg)

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

###  Alternatives

[stuyam/laravel-phone-validator

A phone validator for Laravel using the free Twilio phone lookup service.

2861.3k](/packages/stuyam-laravel-phone-validator)[skysplit/laravel5-intl-translation

Laravel 5 package for better translation syntax using php-intl extension

10106.2k](/packages/skysplit-laravel5-intl-translation)[pacerit/laravel-polish-validation-rules

Simple Polish Validation rules for Laravel and Lumen framework

1449.9k](/packages/pacerit-laravel-polish-validation-rules)[fab2s/dt0

Immutable DTOs with bidirectional casting. No framework required. 8x faster than the alternative.

101.6k1](/packages/fab2s-dt0)[laravel-validation-rules/ip

Validate if an ip address is public or private.

1629.7k](/packages/laravel-validation-rules-ip)[cohensive/validation

Extra functionality for Laravel 5 Validator.

1513.9k](/packages/cohensive-validation)

PHPackages © 2026

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