https://github.com/webhkp/pvalidate
Simple and flexible validator for PHP
https://github.com/webhkp/pvalidate
composer library php8 validation
Last synced: 6 months ago
JSON representation
Simple and flexible validator for PHP
- Host: GitHub
- URL: https://github.com/webhkp/pvalidate
- Owner: webhkp
- Created: 2024-05-22T13:20:20.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-29T12:34:42.000Z (almost 2 years ago)
- Last Synced: 2025-08-14T17:54:24.016Z (11 months ago)
- Topics: composer, library, php8, validation
- Language: PHP
- Homepage: https://webhkp.com/pvalidate
- Size: 61.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pvalidate




Pvalidate is a robust and flexible validation library for PHP. It is designed to simplify the process of validating data, ensuring data integrity, and enforcing business rules within your PHP applications.
Whether you are building a small application or a large enterprise system, Pvalidate provides the tools you need to validate your data effectively and efficiently.
### [Check the full documentation here](https://webhkp.com/pvalidate)
### [Check the package on packagist.org](https://packagist.org/packages/webhkp/pvalidate)
## Installation
```bash
composer require webhkp/pvalidate
```
## Usage
```php
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());
```
## Output
```
bool(false)
array(5) {
["address"]=>
array(3) {
["value"]=>
NULL
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["required"]=>
string(25) "address field is Required"
}
}
["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)"
}
}
["myArr"]=>
array(3) {
["value"]=>
array(0) {
}
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["required"]=>
string(23) "myArr field is Required"
}
}
}
array(5) {
[0]=>
string(25) "address field is Required"
[1]=>
string(42) "prop1 should be larger than or equal to 50"
[2]=>
string(53) "prop2 should not be in the disallowed list (1,2,3,40)"
[3]=>
string(23) "myArr field is Required"
}
```
## Individual Rule Usage
### Use "Allow" Rule
```php
safeParse(22);
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
```
Output:
```
bool(false)
array(1) {
["allowed"]=>
string(45) " should be in the allowed list (1,2,3,4,5,10)"
}
```
### Use 'Regex' Rule
```php
safeParse('D2ab');
var_dump($regexResult->isValid());
var_dump($regexResult->getErrors());
```
Output:
```
bool(false)
array(1) {
["regex"]=>
string(42) " should match the regex '/^[A-Z0-9]{3}.*/'"
}
```
## Parse and Throw Error
```php
parse(null);
var_dump($requiredValidationResult->isValid());
var_dump($requiredValidationResult->getErrors());
} catch (\Exception $e) {
var_dump($e->getMessage());
}
```
Output:
```
object(Webhkp\Pvalidate\Exceptions\PvalidateException)#8 (8) {
["message":protected]=>
string(18) " field is Required"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(1)
["file":protected]=>
string(80) "package/pvalidate/src/Rules/ValidationRule.php"
["line":protected]=>
int(72)
["trace":"Exception":private]=>
array(1) {
[0]=>
array(5) {
["file"]=>
string(43) "index.php"
["line"]=>
int(126)
["function"]=>
string(5) "parse"
["class"]=>
string(37) "Webhkp\Pvalidate\Rules\ValidationRule"
["type"]=>
string(2) "->"
}
}
["previous":"Exception":private]=>
NULL
["additionalData":"Webhkp\Pvalidate\Exceptions\PvalidateException":private]=>
NULL
}
```
## Custom Rule
```php
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());
```
Output:
```
bool(false)
array(2) {
["myArr"]=>
array(3) {
["value"]=>
array(0) {
}
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["required"]=>
string(23) "myArr field is Required"
}
}
["password"]=>
array(3) {
["value"]=>
string(12) "mysimplepass"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["password"]=>
string(135) "password is not a valid password (minimum length 100, must contain a uppercase letter, a digit and a special character from "!@#$%^&*")"
}
}
}
```
## Validation Builder
```php
gt(11)->gte(200)->range(min: 1, max: 1000)->safeParse(11);
var_dump($validation->isValid());
var_dump($validation->getErrors());
//var_dump($validation->getMessages());
//var_dump($validation->getResult());
```
Output:
```
bool(false)
array(2) {
["gt"]=>
array(3) {
["value"]=>
int(11)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["min"]=>
string(26) " should be larger than 11"
}
}
["gte"]=>
array(3) {
["value"]=>
int(11)
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["min"]=>
string(38) " should be larger than or equal to 200"
}
}
}
```