PHP code example of blakvghost / php-validator

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

    

blakvghost / php-validator example snippets


use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;

try {

    $data = [
        'username' => 'BlakvGhost',
        'email' => '[email protected]',
        'score' => 42,
    ];
    // or
    // $data = $_POST;

    $validator = new Validator($data, [
        'username' => 'alidatorException $e) {
    echo "Validation error: " . $e->getMessage();
}


$data = [
    'username' => 'BlakvGhost',
];

$validator = new Validator(
    $data,
    [
        'username' => '          'string' => 'Votre nom d\'utilisateur doit forcément être une chaîne de caractère',
            ],
        ]
    );
    
    // For the default language
    $validator = new Validator($data, $rules, lang: 'fr');

    'username' => '

    'username' => 'string'
    

    'email' => 'email'
    

    'username' => 'max:25'
    

    'password_confirmation' => 'confirmed:password'
    

    'file' => 'file'
    

    'terms_and_conditions' => 'accepted'
    

    'terms_and_conditions' => 'accepted_if:is_adult,true'
    

    'website' => 'active_url'
    

    'name' => 'alpha'
    

    'age' => 'numeric'
    

    'username' => 'lowercase'
    

    'username' => 'uppercase'
    

    'role' => 'in:admin,editor,viewer'
    

    'optional_field' => 'nullable'
    

    'password' => 'password'
    

    'password_confirmation' => 'same:password'
    

    'username' => 'min:8'
    

    'value' => 'not_in:foo,bar'
    

    'firstname' => '

    'client_ip' => 'ip',
    

    'config' => 'json',
    

    'website' => 'url',
    

    'pseudo' => 'alpha_num',
    

    'is_admin' => 'bool',
    

        [
            'string' =>'size:7', // strlen(string) == 7
            'integer' =>'size:7', // integer == 7
            'array' =>'size:7', // count(array) == 7
            'file' =>'size:512', // file size (kb) == 512
        ]
    

    'email' => 'not_

// CustomPasswordRule.php
namespace YourNameSpace\Rules;

use BlakvGhost\PHPValidator\Contracts\Rule;

class CustomPasswordRule implements Rule
{
    protected $field;

    public function __construct(protected array $parameters = [])
    {
    }

    public function passes(string $field, $value, array $data): bool
    {
        $this->field = $field;
        // Implement your custom validation logic here
        // Example: Check if the password is equal to confirm_password
        return $value === $data['confirm_password'];
    }

    public function message(): string
    {
        return "Vos deux mot de passes ne sont pas identiques";
    }
}


    use BlakvGhost\PHPValidator\Validator;
    use BlakvGhost\PHPValidator\ValidatorException;

    use YourNameSpace\CustomPasswordRule;


    // ...

    try {

        $data = [
            'password' => '42',
            'confirm_password' => '142',
        ];
        // or
        // $data = $_POST;

        $validator = new Validator($data, [
            'password' => ['


    use BlakvGhost\PHPValidator\Validator;
    use BlakvGhost\PHPValidator\ValidatorException;
    use BlakvGhost\PHPValidator\Mapping\RulesMaped;

    use YourNameSpace\CustomPasswordRule;

    // Add your rule here using an alias and the full namespace of your custom class
    RulesMaped::addRule('c_password', CustomPasswordRule::class);

    try {

        $data = [
            'password' => '42',
            'confirm_password' => '142',
        ];

        $validator = new Validator($data, [
            'password' => '
bash
composer