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

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

iulyanp/flex-validator
======================

A small validator based on Respect Validation library

v0.1.2(8y ago)14162MITPHPPHP ^7.0

Since Jan 24Pushed 8y ago1 watchersCompare

[ Source](https://github.com/iulyanp/flex-validator)[ Packagist](https://packagist.org/packages/iulyanp/flex-validator)[ RSS](/packages/iulyanp-flex-validator/feed)WikiDiscussions master Synced 3d ago

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

Flex Validator
--------------

[](#flex-validator)

[![Latest Stable Version](https://camo.githubusercontent.com/fb90052673f09015a3660e982874421635762987e33a25e03efd4b4486e407a0/68747470733a2f2f706f7365722e707567782e6f72672f69756c79616e702f666c65782d76616c696461746f722f76657273696f6e)](https://packagist.org/packages/iulyanp/flex-validator)[![Total Downloads](https://camo.githubusercontent.com/11902d00479ccfc63ae19c8765027feef84c61be50203aedde0f74c841ace5ba/68747470733a2f2f706f7365722e707567782e6f72672f69756c79616e702f666c65782d76616c696461746f722f646f776e6c6f616473)](https://packagist.org/packages/iulyanp/flex-validator)[![Latest Unstable Version](https://camo.githubusercontent.com/596812f278074ddc98dc9d3f4743bcb4b4d60ad58796a46851654d52a7b11120/68747470733a2f2f706f7365722e707567782e6f72672f69756c79616e702f666c65782d76616c696461746f722f762f756e737461626c65)](//packagist.org/packages/iulyanp/flex-validator)[![License](https://camo.githubusercontent.com/0fb6ce5469014223830e1c85db1f8de20c6c981362c0430bab51d4df0b525ddc/68747470733a2f2f706f7365722e707567782e6f72672f69756c79616e702f666c65782d76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/iulyanp/flex-validator)[![composer.lock available](https://camo.githubusercontent.com/387661ddd19d8f43a0e565b0c6c35a5942361a3ce55b4104a975ab52e15ffeda/68747470733a2f2f706f7365722e707567782e6f72672f69756c79616e702f666c65782d76616c696461746f722f636f6d706f7365726c6f636b)](https://packagist.org/packages/iulyanp/flex-validator)

A simple validator based on [Respect Validation](https://github.com/Respect/Validation) library, that provides an easy way to customize the error messages.

### Installation

[](#installation)

If you have composer installed globally you can run:

```
$ composer require iulyanp/flex-validator

```

or you can go with:

```
$ php composer.phar require iulyanp/flex-validator

```

### Usage

[](#usage)

FlexValidator can validate simple input:

```
$validator = new FlexValidator();
$validator->validate('Flex Validator', ['rules' => v::notBlank()->alnum('_')->noWhitespace()]);

if (!$validator->isValid()) {
    var_dump($validator->getErrors());
}

// do something with the valid data

```

For our little example the `getErrors()` will return something like this:

```
array:2 [
  "notBlank" => """ must not be blank"
  "alnum" => """ must contain only letters (a-z), digits (0-9) and "_""
]

```

But you can also validate arrays like this:

```
// use Respect Validator
use Respect\Validation\Validator;

$array = [
    'name' => 'Iulian Popa',
    'contact' => [
        'phone' => '07579940094',
        'address' => 'str, Ion Creanga nr.14 A bl. N2'
    ]
];

$rules = [
    'name' => Validator::notBlank(),
    'contact.phone' => Validator::notBlank()->numeric()->length(0, 10),
    'contact.address' => Validator::notBlank()->alnum('.')->length(6, 30),
];

$validator = new FlexValidator();
$validator->validate($array, $rules);

if (!$validator->isValid()) {
    var_dump($validator->getErrors());
}

```

The `getErrors` method will return an array with all breaking validation rules:

```
array:1 [
  "contact" => array:2 [
    "phone" => array:1 [
      "length" => ""07579940094" must have a length lower than 10"
    ]
    "address" => array:2 [
      "alnum" => ""str, Ion Creanga nr.14 A bl. N2" must contain only letters (a-z), digits (0-9) and ".""
      "length" => ""str, Ion Creanga nr.14 A bl. N2" must have a length between 6 and 30"
    ]
  ]
]

```

#### Validation methods

[](#validation-methods)

Currently you can validate simple values and multidimensional arrays.

```
$rules = [
    'rules' => Validator::notBlank(),
];

$validator->validate('value', $rules);

```

The `validate` method is a shortcut also for the `array` method which will validate multidimensional arrays.

```
$array = [
    'name' => 'Iulian Popa',
    'contact' => [
        'phone' => '07579940094',
        'address' => 'str, Ion Creanga nr.14 A bl. N2'
    ]
];

$rules = [
    'name' => Validator::notBlank(),
    'contact.phone' => Validator::notBlank()->numeric()->length(0, 10),
    'contact.address' => Validator::notBlank()->alnum('.')->length(6, 30),
];

$validator->array($array, $rules);

```

#### Disable named rules

[](#disable-named-rules)

As we saw in above the examples the error messages had the key of the errors array as the name of the broken rule: `alnum`, `length`.

```
array:1 [
  "contact" => array:2 [
    "address" => array:2 [
      "alnum" => ""str, Ion Creanga nr.14 A bl. N2" must contain only letters (a-z), digits (0-9) and ".""
      "length" => ""str, Ion Creanga nr.14 A bl. N2" must have a length between 6 and 30"
    ]
  ]
]

```

We can disable this with a simple call of the `disableRulesName()` method on the FlexValidator.

```
$validator->disableRulesName();

$validator->validate($array, $rules);

if (!$validator->isValid()) {
    var_dump($validator->getErrors());
}

//    array:1 [
//      "contact" => array:2 [
//        "phone" => array:1 [
//          0 => ""07579940094" must have a length lower than 10"
//        ]
//        "address" => array:2 [
//          0 => ""str, Ion Creanga nr.14 A bl. N2" must contain only letters (a-z), digits (0-9) and ".""
//          1 => ""str, Ion Creanga nr.14 A bl. N2" must have a length between 6 and 30"
//        ]
//      ]
//    ]

```

#### Use dot error keys

[](#use-dot-error-keys)

Also the errors array can be returned with dot keys.

```
$validator->useDotErrorKeys();
$validator->validate($array, $rules);

if (!$validator->isValid()) {
    var_dump($validator->getErrors());
}

//    array:2 [
//        "contact.phone" => array:1 [
//        "length" => ""07579940094" must have a length lower than 10"
//      ]
//      "contact.address" => array:2 [
//        "alnum" => ""str, Ion Creanga nr.14 A bl. N2" must contain only letters (a-z), digits (0-9) and ".""
//        "length" => ""str, Ion Creanga nr.14 A bl. N2" must have a length between 6 and 30"
//      ]
//    ]

```

You can also use the `dot keys` with `disabled rule names` together.

#### Error groups

[](#error-groups)

Sometime when we validate an array of data maybe we want to wrap the error messages in a group. With FlexValidator we can do this in two ways: global group and specific group.

##### Specific group

[](#specific-group)

First let's take a look on how we can specify a specific group on a specific array key.

```
use Respect\Validation\Validator;

$array = [
    'username' => '',
    'password' => '',
];
$rules = [
    'username' => [
        'rules' => Validator::notBlank(),
        'group' => 'login'
    ],
    'password' => [
        'rules' => Validator::notBlank(),
    ]
];

$validator = new FlexValidator();
$validator->validate($array, $rules);

// will return
array:2 [
  "login" => array:1 [
    "username" => array:1 [
      "notBlank" => "null must not be blank"
    ]
  ]
  "password" => array:1 [
    "notBlank" => "null must not be blank"
  ]
]

```

As you can see the field `username` was wrapped in a group `login`.

##### Global group

[](#global-group)

The global group will be applied only on the fields that don't have a specific group set. To specify a global group we can pass the third argument to the `validate` method.

```
use Respect\Validation\Validator;

$array = [
    'username' => '',
    'password' => '',
];
$rules = [
    'username' => [
        'rules' => Validator::notBlank(),
        'group' => 'login'
    ],
    'password' => [
        'rules' => Validator::notBlank(),
    ]
];

$validator = new FlexValidator();
$validator->validate($array, $rules, 'auth');

// will return
array:2 [
  "login" => array:1 [
    "username" => array:1 [
      "notBlank" => "null must not be blank"
    ]
  ]
  "auth" => array:1 [
    "password" => array:1 [
      "notBlank" => "null must not be blank"
    ]
  ]
]

```

#### Custom error messages

[](#custom-error-messages)

Almost all the time when we use a validation library we want to overwrite the default error messages. In the next steps we can see how FlexValidator made this objective very clean and easy to use.

##### Overwrite default error messages from RespectValidation

[](#overwrite-default-error-messages-from-respectvalidation)

We can set the default error messages passing an array with the messages for each respect validation rule.

```
use Respect\Validation\Validator;

$array = [
    'name' => '',
];
$rules = [
    'name' => Validator::notBlank(),
];

$validator = new FlexValidator([
    'notBlank' => 'The value should not be blank'
]);
$validator->validate($array, $rules);

array:2 [
  "name" => array:1 [
    "notBlank" => "The value should not be blank"
  ]
]

```

##### Custom error messages globally for an entire set of data

[](#custom-error-messages-globally-for-an-entire-set-of-data)

We can go further and use custom error messages globally when we validate a set of data by passing the array with error messages as the fourth argument to the `validate` method.

```
use Respect\Validation\Validator;

$array = [
    'name' => '',
    'contact' => [
        'phone' => ''
    ]
];
$rules = [
    'name' => Validator::notBlank(),
    'contact.phone' => Validator::notBlank(),
];

$validator = new FlexValidator([
    'notBlank' => 'The value should not be blank'
]);
$validator->validate($array, $rules, '', [
    'notBlank' => 'The value should not be empty'
]);

// will return
array:2 [
  "name" => array:1 [
    "notBlank" => "The value should not be empty"
  ]
  "contact" => array:1 [
    "phone" => array:1 [
      "notBlank" => "The value should not be empty"
    ]
  ]
]

```

##### Custom error messages on specific value of an array

[](#custom-error-messages-on-specific-value-of-an-array)

If we want to use a custom error message only for a specific field we can do so by specifying the `messages` key as an array with all the custom error messages. Here is an example.

```
use Respect\Validation\Validator;

$array = [
    'name' => '',
    'contact' => [
        'phone' => '',
        'address' => 'str, Ion Creanga nr.14 A bl. N2',
    ],
];

$rules = [
    'name' => Validator::notBlank(),
    'contact.phone' => [
        'rules' => Validator::notBlank()->numeric()->length(0, 10),
        'messages' => [
            'notBlank' => 'Please provide the phone number.',
            'numeric' => 'Your phone number shounld contain only numbers.',
            'length' => 'Your phone number should not be over 10 characters.'
        ],
    ],
];

$validator = new FlexValidator([
    'notBlank' => 'The value should not be blank'
]);
$validator->validate($array, $rules, '', [
    'notBlank' => 'The value should not be empty'
]);

// will return
array:2 [
  "name" => array:1 [
    "notBlank" => "The value should not be empty"
  ]
  "contact" => array:1 [
    "phone" => array:2 [
      "notBlank" => "Please provide the phone number."
      "numeric" => "Your phone number shounld contain only numbers."
    ]
  ]
]

```

As you can see the error message from the `name` field is taken from the fourth argument of the `validate` method and the error messages for the `phone` number are taken from the `messages` array.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 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 ~10 days

Total

3

Last Release

3011d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/36a79c4e577e9fa16232e332648232b176262e98e7a9e6b6a8b546fa4acd26c7?d=identicon)[iulyanp](/maintainers/iulyanp)

---

Top Contributors

[![iulyanp](https://avatars.githubusercontent.com/u/6533422?v=4)](https://github.com/iulyanp "iulyanp (14 commits)")

---

Tags

flex-validatorflexible-validatorrespectrespect-validationvalidationvalidatorvalidatorvalidationrespect validationflex validator

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[opis/json-schema

Json Schema Validator for PHP

64236.9M186](/packages/opis-json-schema)[vlucas/valitron

Simple, elegant, stand-alone validation library with NO dependencies

1.6k4.4M128](/packages/vlucas-valitron)[intervention/validation

Additional validation rules for the Laravel framework

6826.7M8](/packages/intervention-validation)[proengsoft/laravel-jsvalidation

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

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[wixel/gump

A fast, extensible &amp; stand-alone PHP input validation class that allows you to validate any data.

1.2k1.3M30](/packages/wixel-gump)

PHPackages © 2026

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