dabrahim/array-validator

Array validator for PHP. Useful to handle $_GET / $_POST arrays.

v1.2.0 2018-10-19 12:15 UTC

This package is auto-updated.

Last update: 2024-09-20 02:36:15 UTC


README

A custom library made to take away the pain of form data validation. Pretty useful for validating $_POST and $_GET arrays

Getting started

  1. PHP 5.4.x is required
  2. composer is required
  3. Change to your working directory and run composer require dabrahim/array-validator

Basic usage

$constraints = array(
    'email' => (object)[
        'prettyName' => 'E-mail',
        'type' => ArrayValidator::TYPE_EMAIL,
    ],
    'firstName' => (object) [
        'prettyName' => 'Prénom',
        'type' => ArrayValidator::TYPE_NAME
    ],
    'lastName' => (object) [
        'prettyName' => 'Nom',
        'type' => ArrayValidator::TYPE_NAME
    ]
);

$a = array(
    'email' => 'john.doe@gmail.com',
    'lastName' => 'Doe',
    'firstName' => 'John'
);

try {
    $av = new ArrayValidator($a, $constraints);
    $av->validate();

    echo "Tout est OK !";

} catch (InvalidValueFormat $e) {
    echo "User error: " .$e->getMessage();

} catch (MissingKeyException $e) {
    echo "User error: " . $e->getMessage();

} catch (Exception $e) {
    echo "Developer error: " . $e->getMessage();
}