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

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

unicframework/validator
=======================

Validator is a server side data validation library for PHP. We can validate html form-data, objects, arrays and json etc.

v1.5.2(5y ago)1602MITPHPPHP &gt;=5.6

Since Dec 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/unicframework/validator)[ Packagist](https://packagist.org/packages/unicframework/validator)[ Docs](https://unicframework.github.io/validator)[ RSS](/packages/unicframework-validator/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (8)DependenciesVersions (9)Used By (0)

Validator
---------

[](#validator)

 [![Validator Logo](logo.jpg)](logo.jpg)

Validator is a server side data validation library for PHP. Validate html form-data, objects, arrays and json etc. Validator make data validation simple.

### Installation

[](#installation)

- Install `composer` if you have not installed.

```
composer require unicframework/validator
```

### Set Validation Rules

[](#set-validation-rules)

We can set data validation rules using `rules` method.

```
use Validator\Validator;

// Set data validation rules
$validator = Validator::make([
  'first_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'last_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'email' => [
    'required' => true,
    'not_null' => true,
    'email' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'in' => ['male', 'female']
  ],
  'password' => [
    'required' => true,
    'not_null' => true,
    'minlength' => 6
  ]
]);
```

We can also use a shorthand method to set data validation rules, which is very simple and shorter.

```
use Validator\Validator;

// Set data validation rules
$validator = Validator::make([
  'first_name,last_name' => 'required|not_null|string',
  'email' => 'required|not_null|email',
  'gender' => 'required|not_null|in:male,female',
  'password' => 'required|not_null|minlength:6'
]);
```

### Set Error Messages

[](#set-error-messages)

We can set error messages using `messages` method. if we don't set error messages then validator automatically generate error messages for you.

```
use Validator\Validator;

// Set validation error messages
$validator = Validator::make([
  'first_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'last_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'email' => [
    'required' => true,
    'not_null' => true,
    'email' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'in' => ['male', 'female']
  ],
  'password' => [
    'required' => true,
    'not_null' => true,
    'minlength' => 6
  ]
],
[
  'first_name' => [
    'required' => 'First name is required',
    'not_null' => 'First name can not be null',
    'string' => 'First name should be in string'
  ],
  'last_name' => [
    'required' => 'Last name is required',
    'not_null' => 'Last name can not be null',
    'string' => 'Last name should be in string'
  ],
  'email' => [
    'required' => 'Email is required',
    'not_null' => 'Email can not be null',
    'email' => 'Please enter valid email address'
  ],
  'gender' => [
    'required' => 'Gender is required',
    'not_null' => 'Gender can not be null',
    'in' => 'Please select valid gender'
  ],
  'password' => [
    'required' => 'Password is required',
    'not_null' => 'Password can not be null',
    'minlength' => 'Password length should be minimum 5 characters'
  ]
]);
```

We can also use a shorthand method to set data validation rules, which is very simple and shorter.

```
use Validator\Validator;

// Set validation error messages
$validator = Validator::make([
  'first_name,last_name' => 'required|not_null|string',
  'email' => 'required|not_null|email',
  'gender' => 'required|not_null|in:male,female',
  'password' => 'required|not_null|minlength:6'
],
[
  'first_name,last_name' => 'required:Name is required|not_null:Name can not be null|string:Name should be in string',
  'email' => 'required:Email is required|not_null:Email can not be null|email:Please enter valid email address',
  'gender' => 'required:Gender is required|not_null:Gender can not be null|in:Please select valid gender',
  'password' => 'required: Password is required|not_null:Password can not be null|minlength:Password length should be minimum 5 characters',
]);
```

### Validate Data

[](#validate-data)

Using validator we can validate html form-data, array, object and json data. Validator validate data according to rules. It will return `true` if all the data are valid, otherwise it will return `false`.

#### Validate single data :

[](#validate-single-data-)

```
use Validator\Validator;

$validator = Validator::make([
  'name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'string' => true,
    'lowercase' => true,
    'in' => ['male', 'female', 'other']
  ],
  'contact.email' => [
    'required' => true,
    'not_null' => true,
    'email' => true,
  ]
],
[
  'contact.email' => [
    'required' => 'Please enter email address.',
    'email' => 'Please enter valid email address.'
  ]
]);

// Data for validation
// We can validate any data like arrays, objects, and json etc.
$data = [
  'name' => 'abc xyz',
  'gender' => 'male',
  'contact' => [
    'email' => 'abc@gmail.com'
  ]
];

// Validate data
if($validator->validate($data)) {
  //Ok data is valid
} else {
  // Display validation errors
  print_r($validator->errors();
}
```

#### Validate multiple sets of data :

[](#validate-multiple-sets-of-data-)

```
use Validator\Validator;

$validator = Validator::make([
  'name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'string' => true,
    'lowercase' => true,
    'in' => ['male', 'female', 'other']
  ],
  'contact.email' => [
    'required' => true,
    'not_null' => true,
    'email' => true,
  ]
],
[
  'contact.email' => [
    'required' => 'Please enter email address.',
    'email' => 'Please enter valid email address.'
  ]
]);

// Data for validation
// We can validate any data like arrays, objects, and json etc.
$data = [
  [
    'name' => 'abc xyz',
    'gender' => 'male',
    'contact' => [
      'email' => 'xyz@gmail.com'
    ]
  ],
  [
    'name' => 'xyz abc',
    'gender' => 'male',
    'contact' => [
      'email' => 'xyz@gmail.com'
    ]
  ]
];

// Validate multiple sets of data
if($validator->validate($data, true)) {
  // Ok data is valid
} else {
  // Display validation errors
  print_r($validator->errors());
}
```

### Get Invalid Errors

[](#get-invalid-errors)

We can get errors using `errors` method. the `errors` method return an array of errors.

```
// Get all errors
$errors = $validator->errors();
```

### Get Valid Data

[](#get-valid-data)

We can get valid parsed data using `getValidData` method. the `getValidData` method return an array of valid data.

```
// Get all valid data
$errors = $validator->getValidData();
```

### Get Invalid Data

[](#get-invalid-data)

We can get invalid parsed data using `getInvalidData` method. the `getInvalidData` method return an array of invalid data.

```
// Get all invalid data
$errors = $validator->getInvalidData();
```

### Set validation rules

[](#set-validation-rules-1)

Validator has a lots of predefined validation rules.

RulesValueDescriptionrequiredbooleanrequired fields check only data exists or not, it doesn't check data is empty or null.nullbooleancheck data is empty or null, use `true` for empty or null and use `false` for non empty or not null values.not\_nullbooleancheck data is empty or null, use `true` for not null and use `false` for empty or null values.alphabetbooleanmatch alphabetical data. use `true` for alphabetical and `false` for non alphabetical values.numericbooleanmatch numeric data. use `true` for numeric and `false` for non numeric values.alphanumericbooleanmatch alphanumeric data. use `true` for alphanumeric and `false` for non alphanumeric values.lowercasebooleanmatch case of string. use `true` for lowercase and `false` for non lowercase values.uppercasebooleanmatch case of string. use `true` for uppercase and `false` for non uppercase values.stringbooleanmatch string data type. use `true` for string and `false` for non string values.integerbooleanmatch integer data type. use `true` for integer and `false` for non integer values.floatbooleanmatch float data type. use `true` for float and `false` for non float values.booleanbooleanmatch boolean data type. use `true` for boolean and `false` for non boolean values.arraybooleanmatch array data type. use `true` for array and `false` for non array values.objectbooleanmatch object data type. use `true` for object and `false` for non object values.jsonbooleanmatch json data type. use `true` for json and `false` for non json values.minlengthintegermatch minimum length of string.maxlengthintegermatch maximum length of string.minintegermatch minimum value of number.maxintegermatch maximum value of number.emailbooleancheck given email is valid email address or not.filebooleancheck data is uploaded file or not.file\_mime\_typearraymatch file mime type in given array.file\_extensionarraymatch file extension in given array.min\_file\_sizebytesmatch minimum file size.max\_file\_sizebytesmatch maximum file size.inarraymatch data in given array.not\_inarraymatch data in given array.equalmixedit will match data with given data.not\_equalmixedit will match data with given data.### Set Custom Rules

[](#set-custom-rules)

We can set predefined/custom rules for data validation. Custom rules take a callback function with one argument. If custom rule return `true` that means data is valid and if it will return `false` that means data is invalid.

```
// Set validation rules
$validator = Validator::make([
  'email' => [
    'required' => true,
    'not_null' => true,
    'email' => true,
    // Set your own custom rules
    'blocked' => function($value) {
      if($value == 'abc@gmail.com') {
        // Email abc@gmail.com is blocked
        return false;
      } else {
        return true;
      }
    },
    // Set your own custom rules
    'available' => is_available($value),
  ]
],
[
  // Set error messages for custom rules
  'email' => [
    'blocked' => 'this email address is blocked',
    'available' => 'this email address is already registered',
  ]
]);
```

License
-------

[](#license)

[MIT License](https://github.com/unicframework/validator/blob/main/LICENSE)

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

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.

###  Release Activity

Cadence

Every ~20 days

Recently: every ~13 days

Total

8

Last Release

1878d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/32831684?v=4)[Rajkumar Dusad](/maintainers/rajkumardusad)[@rajkumardusad](https://github.com/rajkumardusad)

---

Top Contributors

[![rajkumardusad](https://avatars.githubusercontent.com/u/32831684?v=4)](https://github.com/rajkumardusad "rajkumardusad (98 commits)")

---

Tags

data-validationform-validatorjsonphpphp-libraryserver-side-validationvalidation-libraryvalidatorvalidatorform validationdata validationfrom validator

### Embed Badge

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

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

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

6.0k39.0M407](/packages/respect-validation)[seld/jsonlint

JSON Linter

1.3k224.9M254](/packages/seld-jsonlint)[composer/spdx-licenses

SPDX licenses list and validation library.

1.4k190.2M34](/packages/composer-spdx-licenses)[opis/json-schema

Json Schema Validator for PHP

64941.2M259](/packages/opis-json-schema)[laminas/laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria

15847.1M212](/packages/laminas-laminas-validator)[ergebnis/json-schema-validator

Provides a JSON schema validator, building on top of justinrainbow/json-schema.

3629.5M7](/packages/ergebnis-json-schema-validator)

PHPackages © 2026

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