v1.0.2 2019-07-27 09:51 UTC

This package is auto-updated.

Last update: 2020-07-22 21:04:48 UTC


php-validator is a package to provide server-side validations

Build Status

📖 Documentation

💿 Installation

php-validator can be installed via composer, just execute the following command in your project root

composer require aubind97/php-validator

Or you can add the following input in your composer.json file.

"require": {
  "aubind97/php-validator": "^1.0"

📚 Usage

Make sure to use composer autoloading to load the package
Be sure to add the use statement or add the namespace before Validator

Using php-validator in your projet is super simple, here is an example

$validator = (new Validator($_POST))
  ->length('firstname', 2, 100)
  ->length('lastname', 2, 100)
  ->length('nickname', 2, 100)
  ->required('email', 'firstname', 'lastname');

We can validate $_POST or any other arrays

Then, you can check for validation like this


If there are errors, you can get errors messages with


Optionnally, you can pass an associative array for parameters internationalization. The key is the attribute name you passed in the Validator constructor, the value is the translation (FR example below).

  'firstname' => 'prénom'

Advanced features

You can add a filter at the validator creation, to discard not needed arguments

$validator = new Validator($_POST, ['firstname', 'lastname']);

You can also create validator with many rules, to validate all params of a model for instance, and apply rules only is the params are in the array params given at the construction

$validator = new Validator($_POST, ['firstname', 'lastname'], true);

Finally, you can pass IETF locale for internationalization (currently supports en-US, fr-FR)

$validator = new Validator($_POST, ['firstname', 'lastname'], true, 'fr-FR');

📏 Available rules

Here is the list of all available validation rules


Check if the requested params are given

$validator->required('key1', 'key2')


Check if the requested param is a date that follow the specified format

$validator->dateTime('key') // Default format 'Y-m-d H:i:s'
$validator->dateTime('key', 'Y-m-d')


Check if the requested param is an email



Check if the requested param exists in the table (in a database)

$validator->exists('key', 'column', 'table', $pdo) // $pdo is a \PDO connection


Check if the requested param is of the specified extensions

$validator->extension('key', ['jpg', 'png']) // You can add the format you want


Check if the requested param length

$validator->length('key', 3) // more than 3 characters
$validator->length('key', null, 10) // less than 10 characters
$validator->length('key', 3, 10) // between 3 and 10 characters


Check if the requested param correspond to a price value



Check if the requested param is not empty

$validator->notEmpty('key1', 'key2')


Check if the requested param is a numeric



Check if the requested param is a slug (word separated by '-')



Check if the requested param is unique in a table (in a databse)

$validator->unique('key', 'column', 'table', $pdo) // $pdo is a \PDO connection

// the value of 'key' is unique excluding the row with the id equal to 1
$validator->unique('key', 'column', 'table', $pdo, 1)


Check if the requested param is uploaded without errors


🤝 Contributions

This repository is maintained by @aubind97

If you want to improve the system I'd love to merge your PR.