PHP code example of webhkp / pvalidate

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

    

webhkp / pvalidate example snippets




Webhkp\Pvalidate\Rules\Regex;
use Webhkp\Pvalidate\Rules\ValidationRule;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Required;
use Webhkp\Pvalidate\Rules\Range;
use Webhkp\Pvalidate\Rules\Allow;
use Webhkp\Pvalidate\Rules\Disallow;


class MyClass {
    public function __construct(
        #[Required]
        public string $name
    ) {

    }

    #[Required]
    public string $address;

    #[Required]
    public string $description;

    #[Range(min: 50, max: 60)]
    const AGE = 40;

    #[Range(min: 50)]
    public int $prop1 = 40;

    #[Range(max: 10)]
    #[Disallow([1, 2, 3, 40])]
    public int $prop2 = 40;

    #[Range()]
    #[Allow([1, 2, 3, 40])]
    public int $prop3 = 40;

    #[Required]
    public array $myArr = [];

    #[Regex('/[A-Z0-9]+/')]
    public string $regexTestField = 'AB23DE';
}


// Usage
$myObj = new MyClass("Test ABC");
$myObj->description = "Some desc string for testing";

$validationResponse = Validator::validate($myObj);

var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
//var_dump($validationResponse->getResult());

var_dump($validationResponse->getMessages());



ebhkp\Pvalidate\Rules\Allow;

// Check single data with rule only
$validation = new Allow([1, 2, 3, 4, 5, 10]);
$validationResult = $validation->safeParse(22);

var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());



ebhkp\Pvalidate\Rules\Regex;

// Regex validation
$regexRule = new Regex('/^[A-Z0-9]{3}.*/');
$regexResult = $regexRule->safeParse('D2ab');
var_dump($regexResult->isValid());
var_dump($regexResult->getErrors());




ebhkp\Pvalidate\Rules\Required;

// Check red)->parse(null);

    var_dump($  var_dump($e->getMessage());
}




ebhkp\Pvalidate\Rules\Custom;
use Webhkp\Pvalidate\Rules\Regex;
use Webhkp\Pvalidate\Rules\ValidationRule;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Required;
use Webhkp\Pvalidate\Rules\Range;
use Webhkp\Pvalidate\Rules\Allow;
use Webhkp\Pvalidate\Rules\Disallow;


// Define your own rule attribute
#[Attribute]
class MyCustomPasswordRule extends ValidationRule {
    public function __construct(private readonly string $minLength) {

    }

    public function isValid(): bool {
        if (strlen($this->value) >= $this->minLength) {
            return false;
        }

        // Must contain one Upper case letter
        if (!preg_match('/[A-Z]/', $this->value)) {
            return false;
        }

        // Must contain a digit
        if (!preg_match('/[0-9]/', $this->value)) {
            return false;
        }

        // Must contain a special character
        if (!preg_match('/[!@#$%^&*]$/', $this->value)) {
            return false;
        }

        return true;
    }

    public function getErrors(): array {
        $errors = [];

        if (!$this->isValid()) {
            $errors['password'] = $this->name . ' is not a valid password (minimum length ' . $this->minLength . ', must contain a uppercase letter, a digit and a special character from "!@#$%^&*")';
        }

        return $errors;
    }

}

class MyClass {
    public function __construct() {

    }

    #[Required]
    public array $myArr = [];

    #[Regex('/[A-Z0-9]+/')]
    public string $regexTestField = 'AB23DE';

    #[MyCustomPasswordRule(100)]
    public string $password = 'mysimplepass';
}


// Usage
$myObj = new MyClass();

$validationResponse = Validator::validate($myObj);

var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
var_dump($validationResponse->getResult());

var_dump($validationResponse->getMessages());



ebhkp\Pvalidate\ValidationBuilder;

$validation = ValidationBuilder::r_dump($validation->getMessages());
//var_dump($validation->getResult());


bool(false)

array(1) {
  ["regex"]=>
  string(42) " should match the regex '/^[A-Z0-9]{3}.*/'"
}