https://github.com/co0lc0der/validator-component
Validator php component
https://github.com/co0lc0der/validator-component
php php-oop validation validator
Last synced: 11 months ago
JSON representation
Validator php component
- Host: GitHub
- URL: https://github.com/co0lc0der/validator-component
- Owner: co0lc0der
- License: mit
- Created: 2021-04-11T23:06:49.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-08-09T14:54:51.000Z (over 3 years ago)
- Last Synced: 2025-01-13T13:48:58.838Z (about 1 year ago)
- Topics: php, php-oop, validation, validator
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Validator php component
This is easy-to-use php component for validate POST or GET data in your project. See `index.php` for examples.
### Public methods:
- `check()` - inits validation with rules
- `setDB()` - sets DB connection (using [QueryBuilder](https://github.com/co0lc0der/QueryBuilder-component) class)
- `errors()` - returns an array of validation's errors
- `passed()` - returns result of validation (true or false)
### Supported rules:
- `required` - true/false
- `min` - minimal length of value
- `max` - maximal length of value
- `matches` - compares 2 fields
- `int` - check integer type
- `min_value` - minimal value of integer
- `max_value` - maximal value of integer
- `unique` - looking for this value in DB
- `email` - check email correct format
- `regex` - any RegEx checking
## How to use
### 1. Include Validator class and init it. If you use `unique` rule you have to include [QueryBuilder](https://github.com/co0lc0der/QueryBuilder-component) class also (see QueryBuilder README).
```php
require __DIR__ . '/Validator/Validator.php';
$validator = new Validator();
```
or
```php
$config = require __DIR__ . '/QueryBuilder/config.php';
require __DIR__ . '/QueryBuilder/Connection.php';
require __DIR__ . '/QueryBuilder/QueryBuilder.php';
require __DIR__ . '/Validator/Validator.php';
$query = new QueryBuilder(Connection::make($config['database']));
$validator = new Validator($query);
```
### 2. Use `check()` method with rules you need.
```php
$validator->check($_POST, [
'username' => [
'required' => true,
'min' => 2,
'max' => 15,
],
'email' => [
'required' => true,
'email' => true,
'unique' => 'users'
],
'password' => [
'required' => true,
'min' => 3
],
'password_again' => [
'required' => true,
'matches' => 'password'
],
'number' => [
'required' => true,
'max' => 5,
'min_value' => 0,
'max_value' => 15,
],
'date' => [
'required' => true,
'regex' => "/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/" // YYYY-MM-DD
],
'agree' => [
'required' => true,
]
]);
```
form example:
```php
> I agree
```
### 3. Do something if validation is passed or print errors.
```php
if ($validator->passed()) {
// do something
} else {
echo '
- ';
- $error ";
foreach ($validator->errors() as $error) {
echo "
}
echo '
}
```