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

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

grahamsutton/validator
======================

A simple PHP validation library.

v1.2(7y ago)514PHP

Since Jul 8Pushed 7y ago2 watchersCompare

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

READMEChangelog (3)Dependencies (1)Versions (5)Used By (0)

Validator
=========

[](#validator)

The validator is a simple PHP validation library to remove the hassle of creating (often times) ugly validation logic and takes huge inspiration from Laravel's internal validation library.

This library is intended to be lightweight (no dependencies!) and easy-to-use.

[![Build Status](https://camo.githubusercontent.com/33fec0edde66c04c6b22bad1dfcb861a10f9187bc88315854cfd056b586473a0/68747470733a2f2f7472617669732d63692e6f72672f67726168616d737574746f6e2f76616c696461746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/grahamsutton/validator)

Requirements:

- PHP 7.1 and higher

To install, run the following in your project root:

```
$ composer require grahamsutton/validator
```

**Available Rules**

- [required](#required)
- [max](#max)
- [min](#min)
- [int](#int)
- [float](#float)
- [numeric](#numeric)
- [email](#email)
- [boolean](#boolean)
- [accepted](#accepted)
- [array](#array)
- [date](#date)
- [afterDate](#afterDate)
- [beforeDate](#beforeDate)

Two Ways to Use It
------------------

[](#two-ways-to-use-it)

There are two ways to perform validations:

**Chain Methods**

You can chain validation methods together if you want to perform validations...

```
use Validator\Validator;

$is_valid = (new Validator())
    ->required('name', 'Graham')
    ->numeric('age', 33)
    ->email('email', 'grahamsutton2@gmail.com')
    ->int('total', 123)
    ->float('amount', 123.45)
    ->boolean('likes_dinos', true)
    ->min('fav_color', 'green', 3)
    ->max('state', 'FL', 2)
    ->date('birthday', '1980-01-01')
    ->array('fav_nums', [7, 4, 92])
    ->accepted('terms', true)
    ->afterDate('start_date', '2018-05-24', '2018-01-31')
    ->beforeDate('end_date', '2024-05-24', '2218-01-31')
    ->validate();
```

-or-

**Pipe Format (preferred)**

Use can use pipe formatting the same way you do in Laravel if you want to perform multiple validations on a field. This is the preferred method of performing validations and looks better most of the time.

```
use Validator\Validator;

$validator = new Validator([
    'name'        => 'required|min:3|max:15',
    'age'         => 'required|numeric',
    'email'       => 'required|email',
    'total'       => 'required|int',
    'amount'      => 'required|float'
    'likes_dinos' => 'required|boolean',
    'date'        => 'required|date',
    'fav_nums'    => 'required|array',
    'terms'       => 'accepted',
    'start_date'  => 'required|afterDate:2018-01-31',
    'end_date'    => 'required|beforeDate:2218-01-31',
]);

$is_valid = $validator->validate([
    'name'        => 'someone',
    'age'         => 23,
    'email'       => 'someone@example.com',
    'total'       => 123,
    'amount'      => 123.45,
    'likes_dinos' => true,
    'date'        => '2018-02-18 23:00:00',
    'fav_nums'    => [7, 4, 92],
    'terms'       => true,
    'start_date'  => '2018-05-24',
    'end_date'    => '2024-05-24',
]));
```

Retrieving Errors
-----------------

[](#retrieving-errors)

Retrieving errors is simple. When a validation fails to pass, it will be recorded into an array that can be retrieved from two different methods: `getErrors` or `getAllErrors`.

**getErrors: array**

This will return a flat associative array, where the key is the name of the failed field and the value is the first error detected.

**Note**: Notice how some values fail multiple validations, like `name`, but there is only error message per field.

```
use Validator\Validator;

$validator = new Validator([
    'name'        => 'required|min:3|max:15',
    'age'         => 'required|numeric',
    'email'       => 'required|email',
    'total'       => 'required|int',
    'amount'      => 'required|float'
    'likes_dinos' => 'required|boolean',
    'date'        => 'required|date',
    'fav_nums'    => 'required|array',
    'terms'       => 'accepted',
    'start_date'  => 'required|afterDate:2018-01-31',
    'end_date'    => 'required|beforeDate:2218-01-31',
]);

// $is_valid will be false
$is_valid = $validator->validate([
    'name'        => '',                     // invalid
    'age'         => 'string',               // invalid
    'email'       => '@example.com',         // invalid
    'total'       => 123.45,                 // invalid
    'amount'      => 123,                    // invalid
    'likes_dinos' => 12,                     // invalid
    'date'        => 'incorrect'             // invalid
    'fav_nums'    => 'should be an array',   // invalid
    'terms'       => false,                  // invalid
    'start_date'  => '2017-05-24',           // invalid
    'end_date'    => '2324-05-24',           // invalid
]));

$validator->getErrors();

// Returns:
// [
//     'name'        => 'The name field is required.',
//     'age'         => 'The age field must be a numeric value.',
//     'email'       => 'The email field must be a valid email.',
//     'total'       => 'The total field must be an integer.',
//     'amount'      => 'The amount field must be a float.',
//     'likes_dinos' => 'The likes_dinos field must be a boolean value.',
//     'date'        => 'The date field is not a valid date.',
//     'fav_nums'    => 'The fav_nums field must be an array.',
//     'terms'       => 'The terms field must be accepted.',
//     'start_date'  => 'The start_date field value must be after 2018-01-31 00:01:00 (yyyy-mm-dd hh:mm:ss).',
//     'end_date'    => 'The end_date field value must be before 2218-01-31 00:01:00 (yyyy-mm-dd hh:mm:ss).',
// ]
```

**getAllErrors: array**

If you want to get *all* error messages recorded for a all fields, just use `getAllErrors` method. This will return the name of the failed fields as the keys and an array of error messages per failed field as the values.

```
use Validator\Validator;

$validator = new Validator([
    'name'       => 'required|min:3|max:15',
    'age'        => 'required|numeric',
    'email'      => 'required|email',
    'accepted'   => 'required|boolean',
    'date'       => 'required|date'
]);

// $is_valid will be false
$is_valid = $validator->validate([
    'name'       => '',              // invalid
    'age'        => 'string',        // invalid
    'email'      => '@example.com',  // invalid
    'accepted'   => 12,              // invalid
    'date'       => 'incorrect'      // invalid
]));

$validator->getAllErrors();

// Returns:
// [
//     'name' => [
//         'The name field is required.',
//         'The name field must be greater than or equal to 3 characters.'
//     ],
//     'age'      => ['The age field must be a numeric value.'],
//     'email'    => ['The email field must be a valid email.'],
//     'accepted' => ['The accepted field must be a boolean value.'],
//     'date'     => ['The date field is not a valid date.']
// ]
```

Available Rules
---------------

[](#available-rules)

### required

[](#required)

Validates that value is not empty and the field is present.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->required('field_name', 'value')
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'required'
]);

$is_valid = $validator->validate([
    'field_name' => 'value'
]);
```

Default error message: `The {field_name} field is required.`

### max:*int*

[](#maxint)

Determines the max value or string length that a field can have.

If the value provided is an integer, the validation will compare that the provided integer is less than or equal to the specified *int* value.

If the value provided is a string, the validation will compare that the value's string length (determined by PHP's *strlen* function) is less than or equal to the specified *int* value.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->max('field_name', 'value', $max = 8)
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'max:8'
]);

$is_valid = $validator->validate([
    'field_name' => 'value'
]);
```

Default error message: `The {field_name} field must be less than or equal to {int} characters.`

### min:*int*

[](#minint)

Determines the minimum value or string length that a field can have.

If the value provided is an integer, the validation will compare that the provided integer is greater than or equal to the specified *int* value.

If the value provided is a string, the validation will compare that the value's string length (determined by PHP's *strlen* function) is greater than or equal to the specified *int* value.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->min('field_name', 'value', $min = 3)
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'min:3'
]);

$is_valid = $validator->validate([
    'field_name' => 'value'
]);
```

Default error message: `The {field_name} field must be greater than or equal to {int} characters.`

### int

[](#int)

Determines that a value is an integer based on PHP's `is_int` function.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->int('field_name', $value = 123)
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'int'
]);

$is_valid = $validator->validate([
    'field_name' => 123
]);
```

Default error message: `The {field_name} field must be an integer.`

### float

[](#float)

Determines that a value is a float based on PHP's `is_float` function.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->float('field_name', $value = 123.45)
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'float'
]);

$is_valid = $validator->validate([
    'field_name' => 123.45
]);
```

Default error message: `The {field_name} field must be an float.`

### numeric

[](#numeric)

Determines that a value is numeric based on PHP's `is_numeric` function. The value can be a string, as long as it can be cast to a numerical value.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->numeric('field_name', $value = 23)
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'numeric'
]);

$is_valid = $validator->validate([
    'field_name' => 23
]);
```

Default error message: `The {field_name} field must be a numeric value.`

### email

[](#email)

Determines that a value is a valid email address.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->email('field_name', 'value@example.com')
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'email'
]);

$is_valid = $validator->validate([
    'field_name' => 'value@example.com'
]);
```

Default error message: `The {field_name} field must be a valid email.`

### boolean

[](#boolean)

Determines that a value is `true`, `false`, `0`, `1`, `"0"`, or `"1"`.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->boolean('field_name', $value = true)
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'boolean'
]);

$is_valid = $validator->validate([
    'field_name' => true
]);
```

Default error message: `The {field_name} field must be a boolean value.`

### accepted

[](#accepted)

Determines that a value is `true`, `1`, `"1"`, or `"yes"`. This is useful for ensuring that people accept things like terms and conditions for your site. This validation will fail if provided a false value.

`"yes"` will be parsed to lowercase when provided, just in case you provide it capitalized.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->accepted('field_name', $value = true)
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'accepted'
]);

$is_valid = $validator->validate([
    'field_name' => true
]);
```

Default error message: `The {field_name} field must be accepted.`

### array

[](#array)

Determines that a value is an array. Internally, it uses PHP's `is_array` function to determine its truthiness. Empty arrays are considered valid. Add a `required` validation to disallow empty arrays.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->array('field_name', $value = ['value1', 'value2'])
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'array'
]);

$is_valid = $validator->validate([
    'field_name' => ['value1', 'value2']
]);
```

Default error message: `The {field_name} field must be an array.`

### date

[](#date)

Determines that a value can be parsed by PHP's `strtotime` function.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->date('field_name', $value = '2018-07-24 03:30:24')
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'date'
]);

$is_valid = $validator->validate([
    'field_name' => '2018-07-24 03:30:24'
]);
```

Default error message: `The {field_name} field is not a valid date.`

### afterDate:*string*

[](#afterdatestring)

Determines if a value is after the specified date. The *string* parameter can be any value that can be parsed by PHP's `strtotime` function.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->afterDate('field_name', $value = '2018-07-25 03:30:24', $after_date = '2018-07-24 00:00:00')
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'afterDate:2018-07-24 00:00:00'
]);

$is_valid = $validator->validate([
    'field_name' => '2018-07-25 03:30:24'
]);
```

Default error message: `The {field_name} field value must be after {after_date} (yyyy-mm-dd hh:mm:ss).`

### beforeDate:*string*

[](#beforedatestring)

Determines if a value is before the specified date. The *string* parameter can be any value that can be parsed by PHP's `strtotime` function.

```
use Validator\Validator;

$is_valid = (new Validator)
    ->beforeDate('field_name', $value = '2018-07-24 03:30:24', $before_date = '2018-07-25 00:00:00')
    ->validate();
```

or

```
use Validator\Validator;

$validator = new Validator([
    'field_name' => 'beforeDate:2018-07-25 00:00:00'
]);

$is_valid = $validator->validate([
    'field_name' => '2018-07-24 03:30:24'
]);
```

Default error message: `The {field_name} field value must be before {before_date} (yyyy-mm-dd hh:mm:ss).`

License
-------

[](#license)

Validator is licensed under the MIT License - see the LICENSE file for details.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity65

Established project with proven stability

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

Total

3

Last Release

2866d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2e1337f95ca37721e90ed0b9e42b8e0c277d3b96a5caaa6c5a2dd85167953cc1?d=identicon)[grahamsutt12](/maintainers/grahamsutt12)

---

Top Contributors

[![grahamsutton](https://avatars.githubusercontent.com/u/7832785?v=4)](https://github.com/grahamsutton "grahamsutton (17 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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