PHP code example of rwarasaurus / validation

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

    

rwarasaurus / validation example snippets


$validator = new Validation\ArrayValidator();

$validator->addConstraint('first_name', new Validation\Assert\Length(['min' => 3]));

$validator->addConstraint('last_name', new Validation\Assert\Present());

// validate email address with MX lookup
$validator->addConstraint('email', new Validation\Assert\Email(['dns' => true]));

$violations = $validator->validate(['first_name' => 'me']);

echo $violations->count(); // 1
echo count($violations); // 1

print_r($violations->getMessages());

Array
(
    [first_name] => Array
        (
            [0] => first name must be greater than or equal to 3 characters
        )

    [last_name] => Array
        (
            [0] => last name must be present
        )
    ...etc
)