Пример #1
0
use phpv\validator\single\native\TautologicValidator;
use phpv\validator\single\native\AlphaNumericalValidator;
use phpv\validator\single\native\AtLeastOnePositiveValidator;
use phpv\output\printer\HTMLErrorPrinter;
$output = null;
$valid = false;
if (isset($_POST["submit"])) {
    $form = new KeyValueSetValidator(new HTMLErrorPrinter());
    $name = KeyValue::obtainFromHTTP("name");
    $password = KeyValue::obtainFromHTTP("password");
    $gender = KeyValue::obtainFromHTTP("gender");
    $browser = KeyValue::obtainFromHTTP("browser");
    $thoughts = KeyValue::obtainFromHTTP("thoughts");
    $football = KeyValue::obtainFromHTTP("football");
    $tennis = KeyValue::obtainFromHTTP("tennis");
    $swimming = KeyValue::obtainFromHTTP("swimming");
    $nameValidator = new SizeRangeValidator($name, "name `%value%` must be between 3 and 20", 3, 20);
    $nameValidator2 = new AlphaNumericalValidator($name, "name must be alphanumerical");
    $passwordValidator = new SizeRangeValidator($password, "password between 3 and 20", 3, 20);
    $genderValidator = new OneOfTheseValuesValidator($gender, "only 'male' or 'female' are permitted", array("m", "f"));
    $browserValidator = new DiversityValidator($browser, "choose your browser of choice", "-");
    $thoughtsValidator = new SizeRangeValidator($thoughts, "keep thoughts between 20 and 100 chars", 20, 100);
    $footballValidator = new TautologicValidator($football);
    $tennisValidator = new TautologicValidator($tennis);
    $swimmingValidator = new TautologicValidator($swimming);
    $atLeastOneSport = new AtLeastOnePositiveValidator(array($football, $tennis, $swimming), "choose at least 1 sport you lazy arse!", "on");
    $form->addValidator($nameValidator);
    $form->addValidator($nameValidator2);
    $form->addValidator($passwordValidator);
    $form->addValidator($genderValidator);
    $form->addValidator($browserValidator);
Пример #2
0
<?php

include_once '../../src/phpv/autoload.php';
use phpv\input\KeyValue;
use phpv\validator\set\KeyValueSetValidator;
use phpv\validator\single\native\SizeRangeValidator;
use phpv\validator\single\native\AlphaNumericalValidator;
use phpv\output\printer\CLIErrorPrinter;
$form = new KeyValueSetValidator(new CLIErrorPrinter());
$name = KeyValue::obtainFromCLI(0);
$nameValidator = new SizeRangeValidator($name, "the name must contain between 3 and 20 chars [provided > '%value%']", 3, 20);
$nameValidator2 = new AlphaNumericalValidator($name, "the name must not contain weird symbols [provided > '%value%']");
$form->addValidator($nameValidator);
$form->addValidator($nameValidator2);
$output = $form->getValidationOutput();
if ($output->isValid()) {
    fwrite(STDOUT, "Name is valid" . PHP_EOL);
} else {
    fwrite(STDOUT, $output->displayErrors());
}