PHP code example of solophp / validator

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

    

solophp / validator example snippets



use Solo\Validator;

$validator = new Validator();

$data = [
    'email' => '[email protected]',
    'username' => 'john_doe',
    'pin_code' => '1234',
    'age' => 25,
];

$rules = [
    'email' => 't_r($validator->errors());
} else {
    echo "Validation passed!";
}

$rules = [
    'username' => 'ength:4',
    'age' => '

$validator->addCustomRule('even', function ($value, $param, $data) {
    return (int)$value % 2 === 0;
});

// Usage
$rules = ['number' => 'even'];
$messages = ['number.even' => 'The number must be even.'];

// Global messages
$messages = [
    'ormat.'
];
$validator = new Validator($messages);

// Per-validation messages
$errors = $validator->validate($data, $rules, [
    'password.min' => 'Password must be at least 8 characters.'
]);

if ($validator->fails()) {
    foreach ($validator->errors() as $field => $messages) {
        echo "$field: " . implode(', ', $messages);
    }
}

// Default message for 'min' rule:
'The :field must be at least :param characters.'

// Becomes:
'The password must be at least 8 characters.'