PHP code example of renanhangai / validator

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

    

renanhangai / validator example snippets


use libweb\Validator as v;

// Data to be validated
$data = array(
    "name" => "John Doe",
    "age"  => "45",
);
// Validate the data against the rule
$data = v::validate( $data, array(
    "name" => v::s(), //< String validator
    "age"  => v::i(), //< Integer validator
    "address?" => v::s(), //< Optional string validator
));

$data->name === "John Doe";
$data->age === 45;
$data->address === null;

use libweb\Validator as v;

$data = array(
    "name" => "John Doe",
    "age"  => "45",
    "password" => "123456",
    "children" => array(
        array( "name" => "John Doe Jr", "age" => 15 ),
        array( "name" => "Mary Doe", "age" => 17 ),
    ),
);

$data = v::validate( $data, array(
    "name" => v::s(), //< String validator
    "age"  => v::i(), //< Integer validator
    "password" => v::s()->minlen( 6 ),
    "children" => v::arrayOf(array(
        "name" => v::s(),
        "age"  => v::i(),
    )),
));

v::s()->str_replace( "foo", "bar" )->regex('/testbar$/')->minlen(10)
// Will pass on "mytestfoo", "another_testfoo", "testbar", "mytestbar"