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

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

weew/validator
==============

Simple validation library.

v3.8.0(9y ago)02223MITPHP

Since Oct 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/weew/validator)[ Packagist](https://packagist.org/packages/weew/validator)[ RSS](/packages/weew-validator/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (5)Versions (26)Used By (3)

PHP Validator
=============

[](#php-validator)

[![Build Status](https://camo.githubusercontent.com/5c76fe5dd3f7f2d03814e7075172b3dbbd598453d5f77c21d3a6abc27ae1586a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f776565772f76616c696461746f722e737667)](https://travis-ci.org/weew/validator)[![Code Quality](https://camo.githubusercontent.com/993abcbcea45085be33b6d84aa7b2035a0cdc88ac641cb69eab3ac96a6bc6787/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f776565772f76616c696461746f722e737667)](https://scrutinizer-ci.com/g/weew/validator)[![Test Coverage](https://camo.githubusercontent.com/6398495adcda56a589b22af8cc36b1cb5c1452f26e7a96ae55fbff23d5f38927/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f776565772f76616c696461746f722e737667)](https://coveralls.io/github/weew/validator)[![Version](https://camo.githubusercontent.com/eb0ad3052ab410e6f89388f1d1e1f4b1a2256bea14a2d7d08e2b563f65fa1fac/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f776565772f76616c696461746f722e737667)](https://packagist.org/packages/weew/validator)[![Licence](https://camo.githubusercontent.com/489f0ddcb90247b72976bc6b29bc41ff5090f6c2c4a12e399ad3bc576adb0ff9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f776565772f76616c696461746f722e737667)](https://packagist.org/packages/weew/validator)

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Available constraints](#available-constraints)
- [Additional constraint packs](#additional-constraint-packs)
- [Constraints](#constraints)
- [Constraint groups](#constraint-groups)
- [Validator](#validator)
- [Validation result and validation errors](#validation-result-and-validation-errors)
- [Composing a custom validator](#composing-a-custom-validator)
- [Creating a custom validator class](#creating-a-custom-validator-class)
- [Custom constraints](#custom-constraints)
- [Wildcard validation](#wildcard-validation)
- [Property accessors](#property-accessors)
    - [Array accessor](#array-accessor)
    - [Object accessor](#object-accessor)
    - [Getter accessor](#getter-accessor)
- [Contributing constraints](#contributing-constraints)

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

[](#installation)

`composer require weew/validator`

Available constraints
---------------------

[](#available-constraints)

- [Accepted](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/AcceptedConstraint.php)
- [Allowed](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/AllowedConstraint.php)
- [AllowedSubset](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/AllowedSubsetConstraint.php)
- [Alpha](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/AlphaConstraint.php)
- [AlphaNumeric](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/AlphaNumericConstraint.php)
- [Array](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/ArrayConstraint.php)
- [Boolean](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/BooleanConstraint.php)
- [DomainName](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/DomainNameConstraint.php)
- [Email](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/EmailConstraint.php)
- [Equals](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/EqualsConstraint.php)
- [Float](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/FloatConstraint.php)
- [Forbidden](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/ForbiddenConstraint.php)
- [ForbiddenSubset](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/ForbiddenSubsetConstraint.php)
- [Integer](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/IntegerConstraint.php)
- [IP](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/IPConstraint.php)
- [IPv4](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/IPv4Constraint.php)
- [IPv6](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/IPv6Constraint.php)
- [Length](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/LengthConstraint.php)
- [MaxAddress](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/MaxAddressConstraint.php)
- [Max](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/MaxConstraint.php)
- [MaxLength](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/MaxLengthConstraint.php)
- [Min](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/MinConstraint.php)
- [MinLength](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/MinLengthConstraint.php)
- [MinMax](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/MinMaxConstraint.php)
- [MinMaxLength](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/MinMaxLengthConstraint.php)
- [NotEmpty](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/NotEmptyConstraint.php)
- [NotNull](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/NotNullConstraint.php)
- [Nullable](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/NullableConstraint.php)
- [Null](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/NullConstraint.php)
- [Numeric](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/NumericConstraint.php)
- [Regex](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/RegexConstraint.php)
- [Scalar](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/ScalarConstraint.php)
- [String](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/StringConstraint.php)
- [Url](https://github.com/weew/validator/blob/master/src/Weew/Validator/Constraints/UrlConstraint.php)

Additional constraint packs
---------------------------

[](#additional-constraint-packs)

There are additional constraints that you may load trough composer.

- [weew/validator-doctrine-constraints](https://github.com/weew/validator-doctrine-constraints)

Constraints
-----------

[](#constraints)

Constraints are small pieces of validation logic. A constraint can be used on its own, without the validator.

```
$constraint = new EmailConstraint();
// or
$constraint = new EmailConstraint('Custom error message.');
$check = $constraint->check('foo@bar.baz');

if ($check) {
    // valdiation passed
} else {
    echo $constraint->getMessage();
}
```

Constraint groups
-----------------

[](#constraint-groups)

Constraint groups allows you to configure multiple constraints for a single value.

```
$group = new ConstraintGroup('email', [
    new EmailConstraint(),
]);
// or
$group = new ConstraintGroup('email');
$group->addConstraint(new EmailConstraint());
```

Constraint groups can be used to validate data without the validator. The `check` method returns a `ValidationResult` object.

```
$result = $group->check('foo@bar.baz');
```

Validator
---------

[](#validator)

The easiest way to use the validator is by creating a new instance and adding constraints inline. Validator will return a `ValidationResult` object.

```
$validator = new Validator();
$data = ['username' => 'foo', 'email' => 'foo@bar.baz'];

$result = $validator->check($data, [
    new ConstraintGroup('email', [
        new EmailConstraint(),
    ]),
]);
```

Validation result and validation errors
---------------------------------------

[](#validation-result-and-validation-errors)

Validation result is used to group occurring validation errors. Validation errors hold information about the validated properties, their values and the applied constraints.

```
if ($result->isFailed()) {
    foreach ($result->getErrors() as $error) {
        // $error->getSubject()
        // $error->getValue()
        // $error->getMessage()
        // $error->getConstraint()
    }
}
```

Composing a custom validator
----------------------------

[](#composing-a-custom-validator)

You can compose a validator with predefined constraints that will be applied on each validation.

```
$data = ['username' => 'foo', 'email' => 'foo@bar.baz'];
$validator->addConstraint('email', new EmailConstraint());
$validator->addConstraints('username', [
    new AlphaConstraint(),
    new MinMaxLengthConstraint(3, 20),
]);

$result = $validator->check($data);
```

Creating a custom validator class
---------------------------------

[](#creating-a-custom-validator-class)

Configuring validators inline is not always the best solution. Sometimes you might want to create dedicated validator classes. With this library this is very easy to achieve.

```
class UserProfileValidator extends Validator {
    protected function configure() {
        $this->addConstraint('email', new EmailConstraint());
        $this->addConstraints('username', [
            new AlphaConstraint(),
            new MinMaxLengthConstraint(3, 20),
        ]);
    }
}

$data = ['username' => 'foo', 'email' => 'foo@bar.baz'];
$validator = new UserProfileValidator();
$result = $validator->check($data);
```

Custom constraints
------------------

[](#custom-constraints)

Creating a new constraint is a fairly easy task. All you have to do is to implement the `IConstraint` interface. This is an example on how to create a simple constraint that makes sure that a number is within the given range.

```
class MinMaxConstraint implements IConstraint {
    protected $min;
    protected $max;
    protected $message;

    public function __construct($min, $max, $message = null) {
        $this->min = $min;
        $this->max = $max;
        $this->message = $message;
    }

    public function check($value, IValidationData $data = null) {
        if (is_numeric($value)) {
            return $value >= $this->min && $value max;
        }

        return false;
    }

    public function getMessage() {
        if ($this->message !== null) {
            return $this->message;
        }

        return 'Some default error message.';
    }

    public function getOptions() {
        return [
            'min' => $this->min,
            'max' => $this->max,
        ];
    }
}
```

Wildcard validation
-------------------

[](#wildcard-validation)

Imagine you have a similar structure that you want to validate.

```
$input = [
    'items' => [
        ['name' => 'name1'],
        ['name' => null],
        ['name' => 'name3'],
    ],
];
```

In order to validate the `name` property of every single element inside the `items` array, you would have to iterate over the items manually. You could also use a wildcard to target all the values. To wildcard array values, you can use this special character `*`.

```
$result = $validator->addConstraint('items.*.name', new NotNullConstraint());
```

In the example above, result will hold an error with subject `items.1.name`.

Array keys can also be validated using wildcards. You'll have to use a different wildcard character `#`. Be aware that the `#` wildcard character should always be the last path segment. This is wrong `foo.#.bar`, this is ok `foo.bar.#`.

```
$input = [
    'items' => [
        'name1' => 'value1',
        '2' => 'value2',
        'name3' => 'value3',
    ],
];

$result = $validator->addConstraint('items.#', new MinMaxLengthConstraint(3, 5));
```

Result will contain an error with subject `#items.1`. As you see, there is a `#` prefix in front of subjects for wildcard keys. This way you can differentiate between subjects for values and subjects for keys.

Property accessors
------------------

[](#property-accessors)

Validator comes with support for multiple data types.

### Array accessor

[](#array-accessor)

This accessor adds support for array based data sets.

```
$data = ['email' => 'foo@bar.baz'];
$validator->check($data);
```

### Object accessor

[](#object-accessor)

This accessor adds support for object based data sets.

```
$data = new stdClass();
$data->email = 'foo@bar.baz';
$validator->check($data);
```

### Getter accessor

[](#getter-accessor)

This accessor adds support for objects that only allow to access data over getter methods.

```
class User {
    protected $username;
    public function __construct($username) {
        $this->username = $username;
    }
    public function getUsername() {
        return $this->username;
    }
}

$validator->check(new User('foo'));
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 52.9% 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 ~12 days

Recently: every ~2 days

Total

25

Last Release

3595d ago

Major Versions

v0.0.3 → v1.0.02015-11-16

v1.3.0 → v2.0.02016-01-21

v2.2.1 → v3.0.02016-01-26

### Community

Maintainers

![](https://www.gravatar.com/avatar/10b2b854b5829dd13a15967c000ed2119b5faef67aca24d94c653c8ac550d85e?d=identicon)[weew](/maintainers/weew)

---

Top Contributors

[![pascalwenger](https://avatars.githubusercontent.com/u/7711605?v=4)](https://github.com/pascalwenger "pascalwenger (9 commits)")[![rmatil](https://avatars.githubusercontent.com/u/3106589?v=4)](https://github.com/rmatil "rmatil (6 commits)")[![getu-lar](https://avatars.githubusercontent.com/u/5556259?v=4)](https://github.com/getu-lar "getu-lar (1 commits)")[![maximkott](https://avatars.githubusercontent.com/u/2686062?v=4)](https://github.com/maximkott "maximkott (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[magento/magento2-functional-testing-framework

Magento2 Functional Testing Framework

15311.8M36](/packages/magento-magento2-functional-testing-framework)[chaoswey/taiwan-id-validator

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

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

PHPackages © 2026

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