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

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

galen/validator
===============

PHP Validation library

146PHP

Since Oct 3Pushed 12y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

\#Validator

Adding a rule is simple

```
$validator->addRule( $rule, $key = null, $error_if_invalid = null, $error_if_empty = null );

```

Example:

```
 $validator->addRule(
 	new \Validator\Rule\MinLength( 2 ), // Rule
 	'firstname', // Key of the data to validate in the array or object
 	'Your first name must contain atleast 2 letters', // Error to display if firstname is invalid
 	'Please enter your first name' // Error to display if firstname is empty
 );

```

`$rule` The rule to add.

`$key` The key in the array or property in the object to validate

`$error_if_invalid` Error to set if the data supplied doesn't validate

`$error_if_empty` Error to set if the data supplied is empty

- Multiple rules can be added for a key.
- `$key`, `$error_if_invalid`, and `$error_if_empty` are optional arguments. `$key` is only required if you are validating an array or object and not a single value. When adding the first rule to a validator if `$key` is supplied the validator knows you are validating an array or object and will throw an exception if `$key` is left out of subsequent calls to `addRule()`. The reverse is also true.
- Having a separate error for invalid and empty data is convenient. For example, say you have a firstname field that you add rules to requiring it to be between 3 and 10 characters. If a user entered 2 letters for their name and you just had an error stating "Please enter your name" the user can become confused. With this method you can have an error message when they leave a field empty stating "This field is required" and an error message when invalid data is supplied instructing them how to fix it.

---

\##Rules

Rules are classes in the Rule directory. Creating your own rules is easy. Add them to the Rules folder and make sure they extend `\Validator\Rule`. Instructions on how to use a rule should be contained in the Rule file.

---

\##Form validation example

You have a comment form

```

    Name:
    Email:
    Comment:

```

To validate it:

1. Check for `$_POST`
2. Create a new validator
3. Add rules to validate the 3 fields
4. Validate `$_POST['comment']`

```
if ( $_POST ) {
    $validator = new \Validator\Validator;
    $validator->addRule( new \Validator\Rule\MinLength( 2 ), 'name', 'Your name must be atleast 2 characters', 'Please enter your name' );
    $validator->addRule( new \Validator\Rule\Email, 'email', 'Please enter a valid email address' );
    $validator->addRule( new \Validator\Rule\MinLength( 2 ), 'text', 'Your comment must be atleast 2 characters', 'Please add a comment' );
    if ( !$validator->validate( $_POST['comment'] ) ) {
        $error = $validator->getFirstError();
    }
    else {
		// Add comment
    }
}

```

---

\##Filtering

```
$validator->addFilter( $filter, $key = null )

```

You can filter globally or on a key basis. If `$key` is null then the filter is applied globally. `$filter` can be any [callable](http://php.net/manual/en/language.types.callable.php).

```
$validator->addFilter( function( $v ){ return ltrim( $v ); } );
$validator->addFilter( 'trim', 'email' );

```

---

\##Errors

`getErrors()` will return an array of all the errors indexed by the key they were used for. Using our comment form example it might look like this:

```
Array
(
    [name] => Array
        (
            [0] => Your name must be atleast 2 characters
        )

    [email] => Array
        (
            [0] => Please enter a valid email address
        )

    [text] => Array
        (
            [0] => Your comment must be atleast 2 characters
        )
)

```

`getFirstError()` will return the message for the first error encountered.

\###Default empty error

Instead of adding the same "This is a required field" to every rule, you can add a default empty error that will be set anytime empty data is passed.

```
$validator->enableDefaultEmptyError( $error_msg )

```

---

\##Validator data

`getData()` will return the data that was passed for validation after the filters have been applied.

`getUnfilteredData()` will return the data that was passed for validation before the filters have been applied.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

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://www.gravatar.com/avatar/3dd6ab4fb506bd5021908b02102d6c5bc75eaab7a3d2506e7dcf7e372de9c9d2?d=identicon)[galen](/maintainers/galen)

---

Top Contributors

[![galen](https://avatars.githubusercontent.com/u/72232?v=4)](https://github.com/galen "galen (9 commits)")

### Embed Badge

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

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

###  Alternatives

[chaoswey/taiwan-id-validator

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

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

PHPackages © 2026

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