PHPackages                             maxlab/validator - 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. maxlab/validator

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

maxlab/validator
================

This is a simple, flexible, but powerful php library for validation

16

Since Dec 18Compare

[ Source](https://github.com/Maxlab/validator)[ Packagist](https://packagist.org/packages/maxlab/validator)[ RSS](/packages/maxlab-validator/feed)WikiDiscussions Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

RespectValidation for Simple validation of data (post,forms, etc.) ([ENG doc](README.md) | [RU doc](README_RU.md))
==================================================================================================================

[](#respectvalidation-for-simple-validation-of-data-postforms-etc-eng-doc--ru-doc)

What is it?
-----------

[](#what-is-it)

This is a small library based on [RespectValidation](https://github.com/Respect/Validation) to simplify the assignment of validation rules receipt and error. Therefore, if you are not familiar with RespectValidation library, for a greater understanding of the example below, it is recommended to familiarize yourself first with her.

Exemple
-------

[](#exemple)

We got the dataset from $\_POST:

```

    $data = array(
        'first_name' => 'Nikolay',
        'last_name' => 1,
        'addresses' => array(
            2 => array(
                'city' => 'Some City',
                'street' => 'Some Street 4',
                'home' => '4b',
                'apartment' => 2,
                'email' => 'notvalidemail',
            ),
            5 => array(
                'city' => 'Some City',
                'street' => 'Some Street 10',
                'home' => '1a',
                'apartment' => 10,
                'email' => 'valid@email.com',
                'siblings_emails' => array(
                    2 => array(
                        'email' => 'notvalidemail'
                    ),
                    8 => array(
                        'email' => 'valid@email.com'
                    ),
                )
            ),
            10 => array(
                'city' => 'Ясный - 1$$',
                'street' => 'Some Street 10',
                'home' => '1a',
                'apartment' => 10,
                'email' => 'valid@email.com',
                'siblings_emails' => array(
                    2 => array(
                        'email' => 'notvalidemail'
                    ),
                    8 => array(
                        'email' => 'valid@email.com'
                    ),
                )
            ),
        ),
    );

```

Often, in the course of work on the project there is a need to connect your own validator classes.

- In this example, the folder `\Maxlab\Validation\Examples\Rules` is the class Alcinum.php
- Below, we talk RespectValidation the location of the additional rules and exceptions.(see details in the documentation [RespectValidation](https://github.com/Respect/Validation))

```

    \Respect\Validation\Validator::with('\\Maxlab\\Validation\\Examples\\Rules', true);

```

Set validation rules:

```

    $rules = [
        'first_name' => \Respect\Validation\Validator::stringType()
            ->postalCode('RU'),
        'last_name' => \Respect\Validation\Validator::stringType()
            ->notEmpty(),
        '\..*.email' => \Respect\Validation\Validator::email(),
        'addresses.*.city' => \Respect\Validation\Validator::stringType()->length(1, 150)->alcinum('- '),
    ];

```

Define our messages for errors:

```

    $locale = new \Maxlab\Validation\Examples\Lang\Ru();
    $validator = new \Maxlab\Validation\DataValidator($data, $rules, $locale);

    if($errors = $validator->getErrors()) {
        var_dump($errors);
    }

```

Get in $errors:

```
array(3) {
  ["first_name"]=> string(46) "first_name must be a valid postal code on "RU""
  ["last_name"]=> string(51) "должно быть строковым типом"
  ["addresses"]=>
  array(3) {
    [2]=>
    array(1) {
      ["email"]=>
      string(59) "не является валидным email адресом"
    }
    [5]=>
    array(1) {
      ["siblings_emails"]=>
      array(2) {
        [2]=>
        array(1) {
          ["email"]=>
          string(59) "не является валидным email адресом"
        }
        [8]=>
        string(59) "не является валидным email адресом"
      }
    }
    [10]=>
    array(2) {
      ["city"]=>
      string(62) "только символы (А-я), (A-z), цифры и "- ""
      ["siblings_emails"]=>
      array(2) {
        [2]=>
        array(1) {
          ["email"]=>
          string(59) "не является валидным email адресом"
        }
        [8]=>
        string(59) "не является валидным email адресом"
      }
    }
  }
}

```

In the templating engine (twig)
-------------------------------

[](#in-the-templating-engine-twig)

```

    {% if formerrors['addresses'][k]['city'] is defined %}

            {{ formerrors['addresses'][k]['city'] }}

    {% endif %}

```

Explanation
-----------

[](#explanation)

As you can see, the simplicity and power of assignment rules make a regular expression. This feature eliminates the need to copy-paste rules and to describe each field separately. Also, this approach makes it more convenient to assign rules for nested data. Using regexps, you can do crazy things:

- `\..*.email` - to providerwith all the data with a key `email` recursively
- to do so `addresses.*.id` or so `addresses.[\d].id` or even so `addresses.[0-9]{1,3}.id`
- using regular expressions opens wide horizons in terms of binding the validation rules to nested data

Conclusion
----------

[](#conclusion)

This approach fits well for the validation of the nested data. It could be a $\_POST array or the data from the Api, the data from the DB and so on. If you have ideas or suggestions on how to improve or optimize the operation of the library, then you should offer them in the form of a pullrequest.

More examples!
--------------

[](#more-examples)

```

    public function getFormRules($formdata)
    {
        $drule = [];
        $drule['address'] = $formdata['formstate']['new_addr'] == 'no_edit' ? '\.(?!new).*\.' : '\..*\.';

        $rules = [
            'user\..*' => v::stringType(),
            'user.first_name' => v::length(0, 150)->alcinum('\'" '),
            'user.last_name' => v::length(0, 150)->alcinum('\'" '),
            'user.phone' => v::optional(v::length(1, 150)->phone()),
            'user.email' => v::notEmpty()->length(1, 150)->email(),

            'user.b_day' => [
                'rule' => v::optional(v::length(1, 2)->between(1, 31)),
                'name' => 'user.birth_day',
            ],
            'user.b_month' => [
                'rule' => v::optional(v::length(1, 2)->between(1, 12)),
                'name' => 'user.birth_day',
            ],
            'user.b_year' => [
                'rule' => v::optional(v::length(1, 4)->between(1910, date('Y'))),
                'name' => 'user.birth_day',
            ],
            'user.birth_day' => v::optional(v::date()),

            'addresses'.$drule['address'].'(?!id|index|comment)' => v::stringType(),
            'addresses'.$drule['address'].'city' => v::notEmpty()->length(1, 255)->alcinum('\'"\\- '),
            'addresses'.$drule['address'].'street' => v::notEmpty()->length(1, 255)->alcinum('\'"\\- '),
            'addresses'.$drule['address'].'home' => v::notEmpty()->length(1, 20),
            'addresses'.$drule['address'].'build' => v::optional(v::length(1, 20)),
            'addresses'.$drule['address'].'room' => v::optional(v::length(1, 20)),
            'customer_details\..*' => v::stringType()->optional(v::length(1, 500)),

            'change_password.old_password' => v::optional(v::stringType()->length(1, 255)->userPasswordCheck()),
            'change_password.new_password' => v::optional(v::stringType()->length(1, 255)->alnum("#*&%$")->noWhitespace()),
        ];

        return $rules;
    }

```

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

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

### Embed Badge

![Health badge](/badges/maxlab-validator/health.svg)

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

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

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