PHP code example of life996 / phpvalidation

1. Go to this page and download the library: Download life996/phpvalidation 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/ */

    

life996 / phpvalidation example snippets




include_once '../vendor/autoload.php';

use Life96\PhpValidation\ValidateArgumentException;
use Life96\PhpValidation\Validators as vas;
use Life96\PhpValidation\ValidateTools as vat;
use Life96\PhpValidation\ValidateException;

$_GET = [
    'name' => 'life',
    'age' => 66,
    'desc' => 'hello world!!',
    'phone' => '18000000000',
    'email' => '[email protected]',
    'position' => 'PM',
    'role' => [
        'doctor', 'programmer'
    ]
];

$rules = [
    'name' => vas::regex('/^\w{1,10}$/'),
    'age' => vas::number()->isInt()->between(12, 120),
    'desc' => vas::string()->length(0, 100),
    'phone' => vas::phone(),
    'email' => vas::email(),
    'role' => vas::array()->maxCount(100),
    'position' => vas::string()->inArray(['PM', 'employee']),
];

try {

    //根据rules key的顺序返回
    list($name, $age, $desc, $phone, $email, $role, $position) = vat::verifyParams($_GET, $rules);

    var_dump($name, $age, $desc, $phone, $email, $role, $position);

} catch (ValidateArgumentException $e) {
    printf("

try {
    
    $data = '18510000000';
    Validators::phone()->validate($data);

} catch (ValidateException $e) {
    printf("validator: %s, msg: %s\n", $e->getValidator()->name, $e->getMessage());
}

try {
    
    $data = '[email protected]';
    Validators::email()->validate($data);

} catch (ValidateException $e) {
    printf("validator: %s, msg: %s\n", $e->getValidator()->name, $e->getMessage());
}

try {
    
    $data = '123123';
    Validators::regex('/^[0-9]+$/')->validate($data);

} catch (ValidateException $e) {
    printf("validator: %s, msg: %s\n", $e->getValidator()->name, $e->getMessage());
}

try {
    
    $data = '5L2g5aW9LCDkuJbnlYwhIQ==';
    Validators::string()->base64()->validate($data)

} catch (ValidateException $e) {
    printf("validator: %s, msg: %s\n", $e->getValidator()->name, $e->getMessage());
}

try {
    
    $data = '你好, 世界';
    Validators::string()->length(1, 6)->validate($data)

} catch (ValidateException $e) {
    printf("validator: %s, msg: %s\n", $e->getValidator()->name, $e->getMessage());
}

try {
    
    $data = '12';
    Validators::number()->between(1, 100)->validate($data);

} catch (ValidateException $e) {
    printf("validator: %s, msg: %s\n", $e->getValidator()->name, $e->getMessage());
}

try {
    
    $data = 12;
    Validators::number()->isInt()->between(1, 100)->validate($data);

} catch (ValidateException $e) {
    printf("validator: %s, msg: %s\n", $e->getValidator()->name, $e->getMessage());
}