Esempio n. 1
0
<?php

error_reporting(E_STRICT | E_ALL);
ini_alter('display_errors', 'on');
require '../vendor/autoload.php';
// Instantiate validator
$validation = new \Validus\Validus();
// To expose closure rule functionality let's say we want to
// parse "money" property of container and compare result
// to some external values
$target = array('money' => '100 USD', 'moreMoney' => '5 EUR');
$currency = 'USD';
$minimumAmount = '20';
$validation->on('money')->closure(function ($money) use($currency, $minimumAmount) {
    $_t = explode(' ', $money);
    if ($_t[0] >= $minimumAmount and $_t[1] == $currency) {
        return true;
    }
    return false;
});
$validation->on('moreMoney')->sameAs('money');
if ($validation->fails($target)) {
    print_r($validation->errors());
}
// As you can see validation fails on moreMoney field, which actually
// does not satisfy our global criteria (min 20 USD)
Esempio n. 2
0
<?php

error_reporting(E_STRICT | E_ALL);
ini_alter('display_errors', 'on');
//include 'SplClassLoader.php';
//$classLoader = new SplClassLoader('Validus', dirname(__FILE__) . '/../src');
//$classLoader->register();
require '../vendor/autoload.php';
// Instantiate validator
$validation = new \Validus\Validus();
// let's say we have this simple «model» container
// which can be object with accessible fields or just
// php asssociative array.
$user = new stdClass();
$user->name = 'John';
$user->surname = 'Doe';
$user->password = '******';
$user->age = 35;
$user->email = '*****@*****.**';
// First of all we want none of our fields to be empty,
// so just apply notEmpty (i.e Rules\NotEmpty object)
// to the whole container, which is StdObject in our case.
$validation->entire($user)->notempty();
// Name and Surname must be from 3 to 32 characters long
// Please note, that this will be equivalent to
// $validation->on('name')->minlength(3)->maxlength(32);
// $validation->on('surname')->sameAs('name');
$validation->on(array('name', 'surname'))->minlength(3)->maxlength(32);
// Age must be between 30 and 40 (just for the sake of demonstration)
$validation->on('age')->gt(30)->lt(40);
// Validate email
Esempio n. 3
0
<?php

error_reporting(E_STRICT | E_ALL);
ini_alter('display_errors', 'on');
require '../vendor/autoload.php';
// Instantiate validator
$validation = new \Validus\Validus();
$target = new stdClass();
$target->name = 'Hal';
$target->invalidName = 'Hal9000';
$validation->on('name')->regexp('/^([a-zA-Z]*)$/', "Can not contain numbers, only characters a-zA-Z");
$validation->on('invalidName')->sameAs('name');
$validation->fails($target);
print_r($validation->errors());