PHP code example of vection-framework / validator

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

    

vection-framework / validator example snippets


$validator = new Vection\Component\Validator\Validator\Date("H:i:s");

# Each validator returns null on success or an object of Violation on fail
$violation = $validator->validate("17:12-43");

if( $violation ){
    $violation->getMessage();   // or
    $violation->getValue();     // or
    echo $violation; // Date "17:12-43" is invalid or does not match format "H:i:s".
} 

// e.g. the POST request input
$data = [
    'name' => 'John Doe',
    'age' => 42,
    'date' => '2019-02-03'
];
    
$chain = new Vection\Component\Validator\ValidatorChain();
    
$chain('name')
    ->notNull()
    ->alphaNumeric()
    ->betweenLength(3, 20)
;

$chain('age')
    ->notNull()
    ->numeric()
    ->range(0, 100)
;

$chain('date')
    ->nullable()
    ->date("Y-m-d")
;

$chain->verify($data);

if( $violations = $chain->getViolations() ){
    //here we got an array of Violation objects
    print json_encode($violations);
    # output: {"name": {"value":"2019-02-03", "message":"..."}, "age": {...}, ...}
} 

class CustomValidator extends Vection\Component\Validator\Validator { ... }

$customValidator = new CustomValidator(...);
$customValidator->validate(...);

// or

$chain = new ValidatorChain();

$chain('xxx')
    ->notNull()
    ->use($customValidator)
;