PHP code example of andrewdyer / slim3-validator

1. Go to this page and download the library: Download andrewdyer/slim3-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andrewdyer / slim3-validator example snippets


use Slim\App;
use Anddye\Validation\Validator;
use Respect\Validation\Validator as v;

$app = new App();

$container = $app->getContainer();

$container['validationService'] = function () {
    return new Validator();
};

$app->get('/', function (Request $request, Response $response) use ($container) {
    $validation = $container['validationService']->validate($request, [
        'email' => v::email()->length(1, 254)->notEmpty(),
        'forename' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
        'password' => v::length(8, 100)->notEmpty(),
        'surname' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
        'username' => v::alnum()->length(1, 32)->notEmpty()->noWhitespace(),
    ]);

    if (!$validation->hasPassed()) {
        // Validation has not passed
    } else {
        // Validation has passed
    }
});

$app->run();

$container['validationService'] = function () {
    return new \Anddye\Validation\Validator();
};

$validation = $container['validationService']->validate($request, [
    'email' => v::email()->length(1, 254)->notEmpty(),
    'forename' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
    'password' => v::length(8, 100)->notEmpty(),
    'surname' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
    'username' => v::alnum()->length(1, 32)->notEmpty()->noWhitespace(),
]);

use Respect\Validation\Validator as v;

if (!$validation->hasPassed()) {
    // Validation has not passed
} else {
    // Validation has passed
}

foreach ($validation->getErrors() as $input => $errors) {
    foreach ($errors as $error) {
        echo $error;
    }
}