-
Notifications
You must be signed in to change notification settings - Fork 1
Quick Start
From composer require to a validated form in a few minutes.
Any associative array works — $_POST, $_GET, a decoded JSON body, or a
plain array.
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use InitPHP\Validation\Validation;
$validation = new Validation($_POST);You can also set or merge data later:
$validation->setData(['email' => 'a@b.com']);
$validation->mergeData(['name' => 'Ada']);Each rule() call adds checks for a field. A rule string is a pipe-separated
list, evaluated left to right. rule() is chainable.
$validation
->rule('name', 'required|string')
->rule('email', 'required|mail')
->rule('age', 'integer|range(18...120)');See the Rules Reference for every rule.
validation() runs the whole queue and returns a boolean.
if ($validation->validation()) {
// all good — use the data
} else {
// something failed
}getError() returns the messages from the most recent run.
foreach ($validation->getError() as $message) {
echo $message, "\n";
}
// e.g. "age must be greater than or equal to 18."isValid() returns the same boolean as the last validation() call without
re-running it.
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use InitPHP\Validation\Validation;
$validation = new Validation([
'username' => 'ada',
'email' => 'ada@example.com',
'password' => 'sup3rsecret',
'confirm' => 'sup3rsecret',
'age' => '34',
]);
$valid = $validation
->rule('username', 'required|alphanum|length(3...20)')
->rule('email', 'required|mail')
->rule('password', 'required|length(8...)')
->rule('confirm', 'again(password)')
->rule('age', 'optional|integer|range(18...120)')
->validation();
if ($valid) {
// persist the user
} else {
$errors = $validation->getError();
}Need something the built-in rules do not cover? Pass a callback as the rule and give it a message:
$validation->rule('number', static function ($value): bool {
return ($value % 2) === 0;
}, '{field} must be an even number.');More in Callable & Custom Rules.
- The Validation Lifecycle — the mental model.
- Rules Reference — the full catalogue.
- Recipes — copy-paste patterns for common forms.
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other