PHP code example of crodas / validator

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

    

crodas / validator example snippets


/** @Validate */
class User
{
    /** @Between([18, 99], "Invalid age range, it should be between 18-99") */ 
    protected $age;
    
    /** 
     * @NoWhitespace("Spaces are not allowed")
     * @MinLength([5], "{$value} is too short") 
     * @MaxLength([10], "{$value} is too long")
     */
    public $username;
}

$user = new User;
$user->age = 17;
$user->username = "invalid username";
if (!crodas\Validator\validate($user, $errors)) {
    echo "<h1>There has been an error</h1>";
    foreach ($errors as $property => $error) {
        echo "...\n";
    }
    exit;
}



$val = new crodas\Validator\Init("/classes/", "/tmp/foo.php");

$errors = $val->validate(new User);
if (!empty($errors)) {
  foreach ($errors as $field => $exception) {
     echo "{$field} is not valid ( {$exception} )\n";
  }
}