PHPackages                             originphp/validation - 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. originphp/validation

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

originphp/validation
====================

OriginPHP Validation

2.0.1(4y ago)110.2k[3 issues](https://github.com/originphp/validation/issues)4MITPHPPHP &gt;=7.3.0CI failing

Since Dec 13Pushed 4y ago1 watchersCompare

[ Source](https://github.com/originphp/validation)[ Packagist](https://packagist.org/packages/originphp/validation)[ Docs](https://www.originphp.com)[ RSS](/packages/originphp-validation/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (2)Versions (15)Used By (4)

Validation
==========

[](#validation)

[![license](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)[![build](https://github.com/originphp/validation/workflows/CI/badge.svg)](https://github.com/validation/collection/actions)[![coverage](https://camo.githubusercontent.com/8ef10dea6596868764e615f33beeed8f0838252317980309451035110de48b5f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6f726967696e7068702f76616c69646174696f6e2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/originphp/validation?branch=master)

This provides a `Validation` library and the `Validator` class for setting up and running validation rules on arrays of data.

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

[](#installation)

To install this package

```
$ composer require originphp/validation

```

Validation Library
------------------

[](#validation-library)

For example

```
$bool = Validation::ip('192.168.1.25');
```

The full list of validation rules are listed below, after the next section.

Validator
---------

[](#validator)

To validate an array of values

```
$validator = new Validator();

$validator->add('name','required')
          ->add('email',[
              'optional', // rule name
              'email' => [
                  'rule' => 'email' // rule name
                  'message' => 'Invalid email address' // custom message
              ]
          ]);

$errors = $validator->validate($_POST);
```

Rule options:

- rule: name of rule, array, callbable e.g. required, numeric, \['date', 'Y-m-d'\],\[$this,'method'\]
- message: the error message to show if the rule fails, if not supplied a default one will be used.
- on: default:null. set to create or update to run the rule only on those, works with second argument of `validate`
- allowEmpty: default:false validation will be pass on empty values
- stopOnFail: default:false wether to continue if validation fails

You can also use custom validation rules using

```
$validator->add('first_name', 'uniqueRuleName', [
        'rule' => [new NameAssert(),'firstName'] // [$object,'method']
]);
```

Or to use a closure

```
$validator->add('status', 'uniqueRuleName', [
      'rule' => function ($value) {
          return $value === 'active';
      }
  ]);
```

The `Validator` uses the `Validation` rules and has 3 additional validation rules built in, `required`, `optional`, and `present`.

These rules should be called first, and for `required` and `present`, if they fail no further validation rules will run.

- required: this means the key must be `present` and the value is `notEmpty`
- optional: this means that data is optional, if the value is `empty` then it will not run any more validation rules.
- present: this means that the data array must have the key present, its irrelevant if the value is empty or not.

ValidateTrait
-------------

[](#validatetrait)

You can add validation any Plain Old PHP Object.

```
class Popo
{
    use ValidateTrait;

    public $name;
    public $email;

    public function __construct()
    {
        $this->validate('name','required');
        $this->validate('email',[
            'required',
            'email'
        ]);
    }
}
```

Then you can check its valid

```
$popo = new Popo();
$popo->name = 'foo';
$popo->email = 'foo@example.com';

if(!$popo->validates()){
    $errors = $popo->errors();
}
```

You can

Validation Rules
----------------

[](#validation-rules)

### accepted

[](#accepted)

Validates a value is accepted (checkbox is checked)

```
Validation::accepted($_POST['accepted']);
```

### after

[](#after)

Validates a date is after a certain date, dates are passed to the `strtotime` function.

```
Validation::after('2019-01-01','now');
```

### alpha

[](#alpha)

Validates a string only contains alphabetic characters from the default locale

```
Validation::alpha('abc');
```

### alphaNumeric

[](#alphanumeric)

Validates a string only contains alphanumeric characters from the default locale

```
Validation::alphaNumeric('abc1234');
```

### array

[](#array)

Validates a value is an array

```
Validation::array([]);
```

### before

[](#before)

Validates a date is before a certain date, dates are passed to the `strtotime` function.

```
Validation::before('2019-01-01','today');
```

### boolean

[](#boolean)

Validates a value is a boolean type

```
Validation::boolean(true);
```

### creditCard

[](#creditcard)

Validates a credit card number

> All regex rules written from scratch using current IIN ranges, so whilst they are accurate the rules are not mature yet.

```
Validation::creditCard('2222 9909 0525 7051');
```

### date

[](#date)

Validates a date using a format compatible with the PHP `DateTime` class.

```
Validation::date('2019-01-01');
Validation::date('01/01/2019','d/m/Y');
```

### dateFormat

[](#dateformat)

Validates a date using a format compatible with the PHP `DateTime` class, this is used by `Validation::date`, `Validation::time`, and `Validation::dateTime`.

The format, which is the second argument is required

```
Validation::dateFormat('01/01/2019','d/m/Y');
```

### dateTime

[](#datetime)

Validates a datetime using a format compatible with the PHP DateTime class.

```
Validation::dateTime('2019-01-01 17:23:00');
Validation::dateTime('01/01/2019 17:23','d/m/Y H:i');
```

### decimal

[](#decimal)

Validates a value is a float. Alias for float

```
Validation::decimal(0.007);
Validation::decimal('0.007');
```

### email

[](#email)

Validation for an email address

```
Validation::email('foo@example.com');
```

You can also check that the email address has valid MX records using the `getmxrr` function.

```
Validation::email('foo@example.com',true);
```

### equalTo

[](#equalto)

Validates a value is equal to another value, only values are compared.

```
Validation::equalTo(5,5);
Validation::equalTo('5',5);
```

### extension

[](#extension)

Validates a value has an extension. If an array is supplied it will look

```
Validation::extension('filename.jpg',['jpg','gif']);
```

You can also check a file that has been uploaded with the correct extension.

```
Validation::extension($_FILES['file1'],['jpg','gif']);
```

### float

[](#float)

Validates a value is a float.

```
Validation::float(0.007);
Validation::float('0.007');
```

### fqdn

[](#fqdn)

Validates a string is Fully Qualified Domain Name (FQDN).

```
Validation::fqdn('www.originphp.com');
```

You can also check the DNS records using `checkdnsrr` to ensure that is really valid and not just looks like its valid.

```
Validation::fqdn('www.originphp.com',true);
```

### greaterThan

[](#greaterthan)

Validates a value is greater than

```
Validation::greaterThan(4,1);
```

### greaterThanOrEqual

[](#greaterthanorequal)

Validates a value is greater than or equals a value.

```
Validation::greaterThanOrEqual(4,1);
```

### hex

[](#hex)

Validates a hexadecimal string.

```
Validation::hex('b1816172fd2ba98f3af520ef572e3a47');
```

### hexColor

[](#hexcolor)

Validates a value is a hex color

```
Validation::hexColor('#f5f5f5');
```

### iban

[](#iban)

Validates a value is an IBAN number

```
Validation::iban('DE89 3704 0044 0532 0130 00');
```

### in

[](#in)

Validates a value is in a list

```
Validation::in('foo',['foo','bar']);
```

### integer

[](#integer)

Validates a value is an integer

```
Validation::integer('1');
Validation::integer(1);
```

### ip

[](#ip)

Validates a value is an IP Address, by default it validates as either IPV4 or IPV6.

```
Validation::ip('192.168.1.1');
```

To validate only IPV4 or IPV6

```
Validation::ip('192.168.1.10','ipv4');
Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334','ipv6');
```

### ipRange

[](#iprange)

Validates an IP address is in a range.

```
Validation::ipRange('192.168.1.5','192.168.168.1','192.168.1.10');
```

### json

[](#json)

Validates a value is a JSON string.

```
$data = json_encode('foo');
Validation::json($data);
```

### length

[](#length)

Validates a string has a certain length.

```
Validation::length('foo', 3);
```

### lessThan

[](#lessthan)

Validates a value is less than another a value.

```
Validation::lessThan(3,5);
```

### lessThanOrEqual

[](#lessthanorequal)

Validates a value is less than or equal to another a value.

```
Validation::lessThanOrEqual(5,5);
```

### lowercase

[](#lowercase)

Validates a string is in lowercase.

```
Validation::lowercase('foo');
```

### luan

[](#luan)

Validates a number using the LUAN algoritm.

```
Validation::luan('7992739871');
```

### macAddress

[](#macaddress)

Validates a string is a valid mac address.

```
Validation::macAddress('00:0b:95:9d:00:17');
```

### maxLength

[](#maxlength)

Validates a string has a maximum length.

```
Validation::maxLength('foo',3);
```

### md5

[](#md5)

Validates a string is a MD5 hash.

```
Validation::md5('b1816172fd2ba98f3af520ef572e3a47');
```

You can also allow it to be case insensitive

```
Validation::md5('B1816172FD2BA98F3AF520EF572E3A47',true);
```

### mimeType

[](#mimetype)

Validates a file has a particular mime type.

```
Validation::mimeType('phpunit.xml','text/xml');
Validation::mimeType('import.csv',['text/csv''text/plain']);
Validation::mimeType($_FILES['upload1'],'application/pdf');
```

### minLength

[](#minlength)

Validates a string has a minimum length.

```
Validation::minLength('foo',3);
```

### notBlank

[](#notblank)

Validates a value is not empty and has anything other than whitespaces.

```
Validation::notBlank('foo');
```

### notEmpty

[](#notempty)

Validates a value is not empty, a value is empty

- `null`
- empty string `''`
- an empty array
- empty file upload

```
Validation::notEmpty('foo');
```

### notIn

[](#notin)

Validates a value is not in an array of values.

```
Validation::notIn('fooz',['foo','bar']);
```

### numeric

[](#numeric)

Validates a value is an integer or a float.

```
Validation::numeric('1');
Validation::numeric(1);
Validation::numeric(9.99);
Validation::numeric('9.99');
```

### present

[](#present)

Validates an array has a key.

```
$data = ['foo'=>'bar'];
Validation::present($data,'foo');
```

### range

[](#range)

Validates a number or float is within a range.

```
Validation::range(5,1,10);
Validation::range('5',1,10);
```

### regex

[](#regex)

Validates a string using a REGEX pattern.

```
Validation::regex('foo','/foo/');
```

### string

[](#string)

Validates that a value is a string

```
Validation::string('foo);
```

### time

[](#time)

Validates a string is a time.

```
Validation::time('10:20');
Validation::time('10:20:00','H:i:s');
```

### upload

[](#upload)

Validates a file upload was successful.

```
Validation::upload($_FILES['upload1']);
Validation::upload($_FILES['upload1']['error']);
```

You can also allow optional file upload, meaning if no file is upload then it will return true if there was no error uploading it.

```
Validation::upload($_FILES['upload1'],true);
```

### uppercase

[](#uppercase)

Validates a string is uppercase.

```
Validation::uppercase('FOO');
```

### url

[](#url)

Validates a string is an URL.

```
Validation::url('www.example.com/q=foo');
Validation::url('http://www.google.com', true);
```

### uuid

[](#uuid)

Validates a string is a UUID

```
Validation::uuid('10458466-a809-4e7a-b784-68d78c25d092');
```

You can also allow uppercase

```
Validation::uuid('86E6E3FC-4924-4B5F-8BCA-E4C07F7CDDF9',true);
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity60

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

Recently: every ~64 days

Total

14

Last Release

1758d ago

Major Versions

0.1.0 → 1.0.02020-05-14

1.6.0 → 2.0.02021-01-04

PHP version history (3 changes)0.1.0PHP ^7.2.0

1.4.1PHP &gt;=7.2.0

2.0.0PHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e8a821333d9c7b7bc2ad3d164d142f65cd3912dea78033d31f76b0f19ba8a0c?d=identicon)[originphp](/maintainers/originphp)

---

Top Contributors

[![jamielsharief](https://avatars.githubusercontent.com/u/20553479?v=4)](https://github.com/jamielsharief "jamielsharief (54 commits)")

---

Tags

validationvalidateMD5credit-cardIBANluhnoriginPHPMac Address

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/originphp-validation/health.svg)

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

###  Alternatives

[intervention/validation

Additional validation rules for the Laravel framework

6826.7M8](/packages/intervention-validation)[wixel/gump

A fast, extensible &amp; stand-alone PHP input validation class that allows you to validate any data.

1.2k1.3M30](/packages/wixel-gump)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)[jschaedl/iban-validation

A small library for validating International BankAccount Numbers (IBANs).

1022.8M16](/packages/jschaedl-iban-validation)[inhere/php-validate

generic data validate, filter library of the php

26787.4k13](/packages/inhere-php-validate)[sadegh19b/laravel-persian-validation

A comprehensive Laravel validation package for Persian text, numbers, dates, and Iranian national identifiers

18293.8k1](/packages/sadegh19b-laravel-persian-validation)

PHPackages © 2026

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