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

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

macfja/validator
================

Validator is a simple way to validate a property or an object.

110PHP

Since Jun 14Pushed 11y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Validator
=========

[](#validator)

**Validator** is a simple way to validate a property or an object.

Features
--------

[](#features)

- Can works with annotation
- Easy to create your own validator
- If compatible, the validator can provide a sanitized version of your value/object

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

[](#installation)

### Composer

[](#composer)

`composer require macfja/validator @dev`

Examples
--------

[](#examples)

### Validate a value

[](#validate-a-value)

```
$myValue = '2014-09-02';
$validator = new LowerThan();
$validator->value = new \DateTime('2012-07-01');
$validator->type = 'date';
$validator->setInput($myValue);

if ($validator->isValid()) {
    die('Houston, we\'ve had a problem');
} else {
    die('You are too old for that');
}
```

### Validate an object (method 1)

[](#validate-an-object-method-1)

Class Person

```
class Person {
    public $firstName = 'John';
    public $lastName = 'Doe';
    public $email = 'jdoe@example.com';
    public $dob;
}
```

Somewhere in your code

```
$jdoe = new Person();
$objectValidator = new ObjectValidator($jdoe);

// - Names validator
$validator1 = new Length();
$validator1->type = 'string';
$validator1->minimum = 1;
$validator1->maximum = 255;//SQL limit?
// - email validator
$validator2 = new Email();
// - age
$validator3 = new DateTime();
$validator4 = new LowerThan();
$validator4->value = '-18 years';
$validator4->type = 'date';

$objectValidator->addValidator('firstName', $validator1);
$objectValidator->addValidator('lastName', $validator1);
$objectValidator->addValidator('email', $validator2);
$objectValidator->addValidator('dob', $validator3);
$objectValidator->addValidator('dob', $validator4);

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

### Validate an object (method 2 - annotation)

[](#validate-an-object-method-2---annotation)

Class Person

```
use MacFJA\Validator\Annotation as Validator

class Person {
    /** @Validator\Length(type="string", minimum=1, maximum=255) */
    public $firstName = 'John';
    /** @Validator\Length(type="string", minimum=1, maximum=255) */
    public $lastName = 'Doe';
    /** @Validator\Email */
    public $email = 'jdoe@example.com';
    /**
      * @Validator\DateTime
      * @Validator\LowerThan("-18 years", type="date")
      */
    public $dob;
}
```

Somewhere in your code

```
$jdoe = new Person();
$objectValidator = new AnnotationValidator($jdoe);

if (!$objectValidator->isValid()) {
    echo sprintf("You have %d error(s) in your object", $objectValidator->getErrorsCount());
}
```

Notice About Annotation
-----------------------

[](#notice-about-annotation)

For annotation validator, you have to:

- add Doctrine Annotations library in your project (`doctrine/annotations`).
- add all annotations into the Doctrine Annotation Registry.

You MUST add all annotations before use it.

To do so you can call the function `AnnotationValidator::registerAnnotations()`. This function will add all default validator annotations into doctrine annotation registry.

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1475671?v=4)[MacFJA](/maintainers/MacFJA)[@MacFJA](https://github.com/MacFJA)

---

Top Contributors

[![MacFJA](https://avatars.githubusercontent.com/u/1475671?v=4)](https://github.com/MacFJA "MacFJA (6 commits)")

### Embed Badge

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

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

###  Alternatives

[chaoswey/taiwan-id-validator

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

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

PHPackages © 2026

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