This documentation provides a detailed explanation of how to use the Webhkp\Pvalidate
validation library to validate class properties and individual values based on various validation rules, including range, allow, disallow, greater than, and less than constraints.
Import required classes-
use Webhkp\Pvalidate\Rules\Builder\RangeRuleBuilder;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Required;
use Webhkp\Pvalidate\Rules\Range;
use Webhkp\Pvalidate\Rules\Allow;
use Webhkp\Pvalidate\Rules\Disallow;
use Webhkp\Pvalidate\Rules\Gt;
use Webhkp\Pvalidate\Rules\Gte;
use Webhkp\Pvalidate\Rules\Lt;
use Webhkp\Pvalidate\Rules\Lte;
use Webhkp\Pvalidate\ValidationBuilder;
Object Validation
Define a class (MyClass
) with properties that require validation:
class MyClass {
public function __construct() {}
#[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;
#[Gt(10)]
public int $gtPropTest1 = 100;
#[Gt(100)]
public int $gtPropTest2 = 100;
#[Gt(900)]
public int $gtPropTest3 = 100;
#[Gte(10)]
public int $gtePropTest1 = 100;
#[Gte(100)]
public int $gtePropTest2 = 100;
#[Gte(900)]
public int $gtePropTest3 = 100;
#[Lt(10)]
public int $ltPropTest1 = 100;
#[Lt(100)]
public int $ltPropTest2 = 100;
#[Lt(900)]
public int $ltPropTest3 = 100;
#[Lte(10)]
public int $ltePropTest1 = 100;
#[Lte(100)]
public int $ltePropTest2 = 100;
#[Lte(900)]
public int $ltePropTest3 = 100;
}
Here, various validation rules are applied to the properties:
Range
: Validates that the value is within the specified range.Allow
: Specifies allowed values.Disallow
: Specifies disallowed values.Gt
: Greater than a specified value.Gte
: Greater than or equal to a specified value.Lt
: Less than a specified value.Lte
: Less than or equal to a specified value.
Create an instance of the class, and validate using the following code-
$myObj = new MyClass();
$validationResult = Validator::validate($myObj);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
This validates the properties of the object against the specified rules. The methods isValid()
and getErrors()
are used to check the validation result and retrieve any validation errors.
Direct Usage of Validation Rules
Validate a value directly using the Range
rule:
$validationResult = (new Range(min: 10))->safeParse(100);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
This checks if the value ‘some string here’ falls within the specified range. Note: Since ‘some string here’ is a string and the range is numeric, this will fail.
$rule = new Range(max: 100);
$validationResult2 = $rule->safeParse(0);
var_dump($validationResult2->isValid());
var_dump($validationResult2->getErrors());
This checks if the value 0
is within the range of 0
to 100
.
Range Rule Builder
This builds a range rule with a minimum value of 100
and checks if 1
falls within this range.
$rule = (new RangeRuleBuilder)->setMin(100)->build();
$validationResult = $rule->safeParse(1);
var_dump($validationResult2->isValid());
var_dump($validationResult2->getErrors());
Range Validation Builder
This checks if the value 500
falls within the range 10
to 100
. Note: Since 500
is a string, this will fail.
$validation = ValidationBuilder::range(10, 100)->safeParse(500);
var_dump($validation->isValid());
var_dump($validation->getErrors());
Greater than (Gt) validation
This checks if 10
is greater than 100
.
$rule = new Gt(100);
$validationResult = $rule->safeParse(10);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
Greater than or equal to (Gte) validation
This checks if 99
is greater than or equal to 100
.
$rule = new Gte(100);
$validationResult = $rule->safeParse(99);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
Less than (Lt) validation
This checks if 1000
is less than 100
.
$rule = new Lt(100);
$validationResult = $rule->safeParse(1000);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
Less than or equal to (Lte) validation
This checks if 100
is less than or equal to 100
$rule = new Lte(100);
$validationResult = $rule->safeParse(100);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
Full Example Code
Here is the full example code-
<?php
require_once "vendor/autoload.php";
use Webhkp\Pvalidate\Rules\Builder\RangeRuleBuilder;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Range;
use Webhkp\Pvalidate\Rules\Allow;
use Webhkp\Pvalidate\Rules\Disallow;
use Webhkp\Pvalidate\Rules\Gt;
use Webhkp\Pvalidate\Rules\Gte;
use Webhkp\Pvalidate\Rules\Lt;
use Webhkp\Pvalidate\Rules\Lte;
use Webhkp\Pvalidate\ValidationBuilder;
class MyClass {
public function __construct() {
}
#[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;
#[Gt(10)]
public int $gtPropTest1 = 100;
#[Gt(100)]
public int $gtPropTest2 = 100;
#[Gt(900)]
public int $gtPropTest3 = 100;
#[Gte(10)]
public int $gtePropTest1 = 100;
#[Gte(100)]
public int $gtePropTest2 = 100;
#[Gte(900)]
public int $gtePropTest3 = 100;
#[Lt(10)]
public int $ltPropTest1 = 100;
#[Lt(100)]
public int $ltPropTest2 = 100;
#[Lt(900)]
public int $ltPropTest3 = 100;
#[Lte(10)]
public int $ltePropTest1 = 100;
#[Lte(100)]
public int $ltePropTest2 = 100;
#[Lte(900)]
public int $ltePropTest3 = 100;
}
// Usage
$myObj = new MyClass();
$validationResult = Validator::validate($myObj);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
// Use validation rule directly
$validationResult = (new Range(min: 10))->safeParse(100);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
$rule = new Range(max: 100);
$validationResult2 = $rule->safeParse(0);
var_dump($validationResult2->isValid());
var_dump($validationResult2->getErrors());
// Range builder
$rule = (new RangeRuleBuilder)->setMin(100)->build();
$validationResult = $rule->safeParse(1);
var_dump($validationResult2->isValid());
var_dump($validationResult2->getErrors());
// Use Validation builder
$validation = ValidationBuilder::range(10, 100)->safeParse(111);
var_dump($validation->isValid());
var_dump($validation->getErrors());
// var_dump($validation->getMessages());
// Gt - Greater than
$rule = new Gt(100);
$validationResult = $rule->safeParse(10);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
// Gte - Greater than or equal to
$rule = new Gte(100);
$validationResult = $rule->safeParse(99);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
// Lt - Less than
$rule = new Lt(100);
$validationResult = $rule->safeParse(1000);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
// Lte - Less than or equal to
$rule = new Lte(100);
$validationResult = $rule->safeParse(100);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
Output:
bool(false)
array(8) {
["prop1"]=>
array(3) {
["value"]=>
int(40)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["min"]=>
string(42) "prop1 should be larger than or equal to 50"
}
}
["prop2"]=>
array(3) {
["value"]=>
int(40)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["disallowed"]=>
string(53) "prop2 should not be in the disallowed list (1,2,3,40)"
}
}
["gtPropTest2"]=>
array(3) {
["value"]=>
int(100)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["min"]=>
string(38) "gtPropTest2 should be larger than 100"
}
}
["gtPropTest3"]=>
array(3) {
["value"]=>
int(100)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["min"]=>
string(38) "gtPropTest3 should be larger than 900"
}
}
["gtePropTest3"]=>
array(3) {
["value"]=>
int(100)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["min"]=>
string(50) "gtePropTest3 should be larger than or equal to 900"
}
}
["ltPropTest1"]=>
array(3) {
["value"]=>
int(100)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["max"]=>
string(38) "ltPropTest1 should be smaller than 10"
}
}
["ltPropTest2"]=>
array(3) {
["value"]=>
int(100)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["max"]=>
string(39) "ltPropTest2 should be smaller than 100"
}
}
["ltePropTest1"]=>
array(3) {
["value"]=>
int(100)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["max"]=>
string(50) "ltePropTest1 should be smaller than or equal to 10"
}
}
}
bool(true)
array(0) {
}
bool(true)
array(0) {
}
bool(true)
array(0) {
}
bool(false)
array(1) {
["range"]=>
array(3) {
["value"]=>
int(111)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["max"]=>
string(39) " should be smaller than or equal to 100"
}
}
}
bool(false)
array(1) {
["min"]=>
string(27) " should be larger than 100"
}
bool(false)
array(1) {
["min"]=>
string(38) " should be larger than or equal to 100"
}
bool(false)
array(1) {
["max"]=>
string(28) " should be smaller than 100"
}
bool(true)
array(0) {
}