PHP code example of alejoluc / validation

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

    

alejoluc / validation example snippets




lejoluc\Validation\Validations;
use alejoluc\Validation\Validator;

$validator = new Validator;

$data = [
    'name'  => 'Alejo Lucangeli',
    'mail'  => '[email protected]',
    'token' => 'adsjkgfuy43758vkj',
    'level' => 3
];
$rules = [
    'name'  => new Validations\NotEmpty,
    'mail'  => [new Validations\NotEmpty, new Validations\Email],
    'token' => new Validations\Regex('/^[a-z0-9]+$/'),
    'level' => [new Validations\Numeric, new Validations\Between(0, 5)]
];

$result = $validator->validate($data, $rules);
if ($result->passes()) {
    echo "All fine";
} else {
    //Display all error messages
    $errors = $result->getErrorMessages();
    foreach ($errors as $error) {
        echo "- $error\n";
    }
    
    //Display errors by field
    $errors = $result->getErrors();
    foreach ($errors as $field => $errorMessages) {
        echo "Errors in field $field:\n";
        foreach ($errorMessages as $message) {
            echo "  - $message\n";
        }
    }
}



lejoluc\Validation\Validations;
use alejoluc\Validation\ValidatorFacade as Validator;

//[... setup data and rules ]

/* @var alejoluc\Validation\ValidationResult $result */
$result = Validator::validate($data, $rules);
if ($result->passes()) {
    echo "All fine";
} else {
    $errors = $result->getErrorMessages();
    foreach ($errors as $error) {
        echo "- $error\n";
    }
}



lejoluc\Validation\Validations;
use alejoluc\Validation\Validator;

$validator = new Validator;
$validator->setLanguage('es-es');

//....