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

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

popphp/pop-validator
====================

Pop Validator Component for Pop PHP Framework

4.8.1(5mo ago)518.3k↑106.3%2BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Jul 17Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/popphp/pop-validator)[ Packagist](https://packagist.org/packages/popphp/pop-validator)[ Docs](https://github.com/popphp/pop-validator)[ RSS](/packages/popphp-pop-validator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (45)Used By (2)

pop-validator
=============

[](#pop-validator)

[![Build Status](https://github.com/popphp/pop-validator/workflows/phpunit/badge.svg)](https://github.com/popphp/pop-validator/actions)[![Coverage Status](https://camo.githubusercontent.com/9c3466e01c93462794e2895614befda04230d1fd02d05893de7f1c93437c8a18/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f702d76616c696461746f72)](http://cc.popphp.org/pop-validator/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Quickstart](#quickstart)
- [Validation Sets](#validation-sets)
    - [Conditions](#conditions)
    - [Rules](#rules)

Overview
--------

[](#overview)

`pop-validator` is a component for validating values and returning the appropriate result messaging. The component comes with a set of built-in evaluation objects and also the ability to extend the component and build your own.

`pop-validator` is a component of the [Pop PHP Framework](http://www.popphp.org/).

[Top](#pop-validator)

Install
-------

[](#install)

Install `pop-validator` using Composer.

```
composer require popphp/pop-validator

```

Or, require it in your composer.json file

```
"require": {
    "popphp/pop-validator" : "^4.6.5"
}

```

[Top](#pop-validator)

Quickstart
----------

[](#quickstart)

Here's a list of the available built-in validators, all under the namespace `Pop\Validator\`:

Built-in ValidatorsAcceptedGreaterThanIsJsonAlphaNumericHasAtLeastIsNotEmptyAlphaHasAtMostIsNotNullBetweenIncludeHasCountEqualIsNullBetweenHasCountGreaterThanIsSubnetOfBooleanHasCountGreaterThanEqualLengthBetweenIncludeContainsHasCountLessThanLengthBetweenCountEqualHasCountLessThanEqualLengthGreaterThanEqualCountGreaterThanEqualHasCountNotEqualLengthGreaterThanCountGreaterThanHasOneLengthLessThanEqualCountLessThanEqualHasOneGreaterThanLengthLessThanCountLessThanHasOneGreaterThanEqualLengthCountNotEqualHasOneLessThanLessThanEqualCreditCardHasOneLessThanEqualLessThanDateTimeBetweenIncludeHasOneThatEqualsNotContainsDateTimeBetweenHasOnlyOneNotEmptyDateTimeEqualHasOnlyOneGreaterThanNotEndsWithDateTimeGreaterThanEqualHasOnlyOneGreaterThanEqualNotEqualDateTimeGreaterThanHasOnlyOneLessThanNotInArrayDateTimeLessThanEqualHasOnlyOneLessThanEqualNotInDateTimeLessThanHasOnlyOneThatEqualsNotStartsWithDateTimeNotEqualInArrayNumericDeclinedInRegExEmailIpv4RequiredEndsWithIpv6StartsWithEqualIsArraySubnetGreaterThanEqualIsEmptyUrl### Check an email value

[](#check-an-email-value)

```
$validator = new Pop\Validator\Email();

// Returns false
if ($validator->evaluate('bad-email-address')) {
    // Prints out the default message 'The value must be a valid email format.'
    echo $validator->getMessage();
}

// Returns true
if ($validator->evaluate('good@email.com')) {
    // Do something with a valid email address.
}
```

### Validate against a specific value

[](#validate-against-a-specific-value)

```
$validator = new Pop\Validator\LessThan(10);

if ($validator->evaluate(8)) { } // Returns true
```

### Set a custom message

[](#set-a-custom-message)

```
$validator = new Pop\Validator\RegEx(
    '/^.*\.(jpg|jpeg|png|gif)$/i',
    'You must only submit JPG, PNG or GIF images.'
);

// Returns false
if ($validator->evaluate('image.bad')) {
    echo $validator->getMessage();
}
```

Alternatively:

```
$validator = new Pop\Validator\RegEx('/^.*\.(jpg|jpeg|png|gif)$/i');
$validator->setMessage('You must only submit JPG, PNG or GIF images.');

if ($validator->evaluate('image.jpg')) { } // Returns true
```

[Top](#pop-validator)

Validation Sets
---------------

[](#validation-sets)

Validation sets are a way to group validators together to evaluate all of them at one time. With that, a level of strictness can be set to enforce whether or not all the validations have to pass or just some of them.

```
use Pop\Validator\ValidatorSet;

$set = new ValidatorSet();
$set->addValidators(['username' => 'AlphaNumeric']);

if ($set->evaluate(['username' => 'username_123'])) {
    echo 'The username satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

**Multiple Validators**

```
use Pop\Validator\ValidatorSet;

$set = new ValidatorSet();
$set->addValidators(['username' => ['AlphaNumeric' => null, 'LengthGte' => 8]]);

if ($set->evaluate(['username' => 'username_123'])) {
    echo 'The username satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

**Custom Messaging**

```
use Pop\Validator\ValidatorSet;

$set = new ValidatorSet();
$set->addValidators([
    'username' => [
        'AlphaNumeric' => [
            'value'   => null,
            'message' => 'The username can only contain alphanumeric characters.'
        ],
        'LengthGte' => 8
    ]
]);

if ($set->evaluate(['username' => 'username_123'])) {
    echo 'The username satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

#### Lazy-Loading vs Eager-Loading

[](#lazy-loading-vs-eager-loading)

If the validators are added to the set validator using the `add*` methods will store the validator configuration and not create the validator objects until the validator set is evaluated (lazy-loading.) Using the `load*` methods, actual validator objects will be stored in the instance of the validator set object (eager-loading.)

#### Strictness

[](#strictness)

The strictness of the validator set can be set as needed. If set to `STRICT_NONE` then only one validator in the set would have to pass in order for the whole set to pass. The same applies for conditions as well:

```
use Pop\Validator\ValidatorSet;

$set = ValidatorSet();
$set->addValidators(['username' => ['AlphaNumeric' => null, 'LengthGte' => 8]]);
$set->setStrict(ValidatorSet::STRICT_NONE);

if ($set->evaluate(['username' => 'someuser_!23'])) {
    echo 'The username satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

Available strict constants are:

- `STRICT_NONE`
- `STRICT_VALIDATIONS_ONLY`
- `STRICT_CONDITIONS_ONLY`
- `STRICT_BOTH` (default)

[Top](#pop-validator)

### Conditions

[](#conditions)

Conditions can be added to a validation set that would need to pass in order for the validations to evaluate. Conditions are validators themselves. And the strictness level can be utilized as well to enforce whether or not all the conditions pass or just some of them.

**Note:** By default, conditions store their validation configuration via lazy-loading and do not create the validator object until the condition is evaluated.

```
use Pop\Validator\ValidatorSet;
use Pop\Validator\Condition;

$set = new ValidatorSet();
$set->addCondition(new Condition('client_id', 'Equal', '1'));
$set->addValidator('documents', 'NotEmpty');

$data = [
    'client_id' => 1,
    'documents' => [
        'some_file_1.pdf',
        'some_file_2.pdf',
    ]
];

if ($set->evaluate($data)) {
    echo 'The client order data satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

In the above example, if the value of `client_id` is changed to `2`, the validators will not be evaluated, as the required conditions would not be satisfied.

[Top](#pop-validator)

### Rules

[](#rules)

Rules provide a shorthand way to wire up validations and conditions within the validation set. Rules are colon-separated strings compromised of a `field`, a `validator` and optional `value` and `message` values. The validator should be a `snake_case` format of the class string, e.g. `HasOne` class should be written as `has_one`.

Below is the same example from above, but using rules instead:

```
$set = Pop\Validator\ValidatorSet::createFromRules([
    'username:alpha_numeric',
    'username:length_gte:8'
]);

if ($set->evaluate(['username' => 'someuser_123'])) {
    echo 'The username satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

Conditions can be added using rules as well. Here is the example from above using rules instead:

```
use Pop\Validator\ValidatorSet;

$set = ValidatorSet::createFromRules('documents:not_empty')
    ->addConditionFromRule('client_id:equal:1');

$data = [
    'client_id' => 1,
    'documents' => [
        'some_file_1.pdf',
        'some_file_2.pdf',
    ]
];

if ($set->evaluate($data)) {
    echo 'The client order data satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

#### Rule Messages

[](#rule-messages)

Custom messaging can be passed via the rule format as well, in the 4th position:

```
$set = Pop\Validator\ValidatorSet::createFromRules([
    'username:length_gt:8:The username must be greater than 8 characters.'
]);
```

#### Referencing Fields

[](#referencing-fields)

When writing rules, fields passed into the input data at the time of evaluating the data can be accessed by using brackets:

```
use Pop\Validator\ValidatorSet;

$set = ValidatorSet::createFromRules('value_1:equal:[value_2]');

$data = [
    'value_1' => 'test',
    'value_2' => 'test'
];

if ($set->evaluate($data)) {
    echo 'The data satisfies the requirements.' . PHP_EOL;
} else {
    print_r($set->getErrors());
}
```

[Top](#pop-validator)

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance73

Regular maintenance activity

Popularity31

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity91

Battle-tested with a long release history

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

Recently: every ~1 days

Total

39

Last Release

152d ago

Major Versions

2.1.1 → 3.0.02017-02-22

v2.x-dev → 3.0.22019-01-10

3.2.2 → 4.0.02023-11-09

PHP version history (8 changes)2.0.0PHP &gt;=5.4.0

3.0.0PHP &gt;=5.6.0

3.0.2PHP &gt;=7.1.0

3.2.0PHP &gt;=7.3.0

3.2.1PHP &gt;=7.4.0

4.0.0PHP &gt;=8.1.0

4.1.2PHP &gt;=8.2.0

4.6.5PHP &gt;=8.3.0

### Community

Maintainers

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

---

Top Contributors

[![nicksagona](https://avatars.githubusercontent.com/u/898670?v=4)](https://github.com/nicksagona "nicksagona (91 commits)")

---

Tags

phpvalidatorvalidationpoppop php

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[vlucas/valitron

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

1.6k4.4M128](/packages/vlucas-valitron)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1814.6k11](/packages/popphp-pop-db)

PHPackages © 2026

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