composer require initphp/validationRequires PHP 8.1+ and ext-mbstring.
Create a validator and give it the data to check. Any associative array works —
$_POST, $_GET, a decoded JSON body, or a plain array.
use InitPHP\Validation\Validation;
$validation = new Validation($_POST);
// or set/merge data later
$validation->setData(['email' => 'a@b.com']);
$validation->mergeData(['name' => 'Ada']);Every rule() call queues one or more checks. validation() runs the whole
queue and returns a boolean. rule() is chainable.
$valid = $validation
->rule('email', 'required|mail')
->rule('name', 'required|alpha')
->validation();A rule string is a pipe-separated list. The first field of email above is
checked for required first, then mail.
getError() returns the messages produced by the most recent
validation() call.
if (!$valid) {
foreach ($validation->getError() as $message) {
echo $message, "\n";
}
}isValid() returns the boolean result of that same run without re-running it.
validation() does three things in order:
- Clears the error list.
- Runs every queued rule (skipping optional fields that have no value).
- Empties the rule queue.
Because the queue is consumed, the natural pattern is queue → validate → read, then repeat:
$validation->rule('a', 'integer');
$validation->validation(); // checks only 'a'
$validation->rule('b', 'integer');
$validation->validation(); // checks only 'b'Call clear() to drop any queued rules, optional flags and errors without
validating.
require '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();
}Next: the rules reference.