The following code demonstrates the usage of the Webhkp\Pvalidate
validation library to validate class properties and individual values based on specified validation rules.
Here is the summary of Required rule usage-
- Validator::validate($object): Validates an object’s properties against defined validation rules.
- safeParse($value): Validates a single value based on the specified rule.
- isValid(): Returns a boolean indicating whether the validation passed.
- getErrors(): Returns an array of validation errors, if any.
Import Necessary Classes from Package
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Required;
use Webhkp\Pvalidate\ValidationBuilder;
Object Property Validation
Here, the #[Required]
attribute specifies that the properties must have values (i.e., they cannot be null or undefined).
Define a class (MyClass
) with properties that require validation:
class MyClass {
public function __construct() {
}
#[Required]
public string $firstString = 'my first string';
#[Required]
public int $firstInt = 100;
#[Required]
public int $secondInt;
}
Here, the #[Required]
attribute specifies that the properties must have values (i.e., they cannot be null or undefined).
Create an instance of the class and Validate the object using Validator::validate method:
$validationResponse = Validator::validate($myObj);
var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
This validates the properties of the object against the specified rules (Required
in this case). The methods isValid()
and getErrors()
are used to check the validation result and retrieve any validation errors.
Direct Usage of Validation Rules
This validates the string 'some string here'
to ensure it is not null or empty. Validate a string directly using the Required
rule:
$requiredRuleValidation1 = (new Required())->safeParse('some string here');
var_dump($requiredRuleValidation1->isValid());
var_dump($requiredRuleValidation1->getErrors());
Validate an integer directly using the Required
rule:
$rule = new Required;
$requiredRuleValidation2 = $rule->safeParse(0);
var_dump($requiredRuleValidation2->isValid());
var_dump($requiredRuleValidation2->getErrors());
This validates the integer 0
to ensure it is not null or empty.
Using the Validation Builder
Use ValidationBuilder
for more concise validation:
$validation = ValidationBuilder::required()->safeParse('your string here');
var_dump($validation->isValid());
var_dump($validation->getErrors());
The ValidationBuilder
allows chaining validation rules for more streamlined code. In this case, it ensures that 'your string here'
is not null or empty.
Full Code Example
Here is the full example code-
<?php
require_once "vendor/autoload.php";
use Webhkp\Pvalidate\Rules\Builder\LengthRuleBuilder;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Required;
use Webhkp\Pvalidate\Rules\Length;
use Webhkp\Pvalidate\ValidationBuilder;
// #[Attribute]
class MyClass {
public function __construct() {
}
#[Required]
public string $firstString = 'my first string';
#[Required]
public int $firstInt = 100;
#[Required]
public int $secondInt;
}
// Demo
// Create object
$myObj = new MyClass();
// Validate the object
$validationResponse = Validator::validate($myObj);
var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
// Use required validation rule directly
$requiredRuleValidation1 = (new Required())->safeParse('some string here');
var_dump($requiredRuleValidation1->isValid());
var_dump($requiredRuleValidation1->getErrors());
$rule = new Required;
$requiredRuleValidation2 = $rule->safeParse(0);
var_dump($requiredRuleValidation2->isValid());
var_dump($requiredRuleValidation2->getErrors());
// Use Validation builder
$validation = ValidationBuilder::required()->safeParse('your string here');
var_dump($validation->isValid());
var_dump($validation->getErrors());
// var_dump($validation->getMessages());
Output:
bool(false)
array(1) {
["secondInt"]=>
array(3) {
["value"]=>
NULL
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["required"]=>
string(27) "secondInt field is Required"
}
}
}
bool(true)
array(0) {
}
bool(false)
array(1) {
["required"]=>
string(18) " field is Required"
}
bool(true)
array(0) {
}