Skip to content
Muhammet Şafak edited this page Jun 10, 2026 · 2 revisions

InitPHP Validation — Wiki

Welcome to the official documentation for initphp/validation — a fast, dependency-free data-validation library for PHP 8.1+.

You describe what each field should look like with a small rule DSL, callbacks, or your own named rules, and get back localized error messages.

composer require initphp/validation
use InitPHP\Validation\Validation;

$validation = new Validation([
    'name' => 'Muhammet',
    'year' => '2022',
]);

$valid = $validation
    ->rule('name', 'required|string')
    ->rule('year', 'integer|range(1970...2099)')
    ->validation();

if (!$valid) {
    foreach ($validation->getError() as $message) {
        echo $message, "\n";
    }
}

The package in one table

Symbol Role
Validation The single entry point: holds the data, the rule queue, the errors and the active locale.
extend() Register a reusable named rule usable from the DSL string.
ExceptionInterface Marker implemented by every exception the package throws.
UndefinedRuleException Thrown when a rule name is unknown — unknown rules fail loudly, they never silently pass.

Start here

What you get

  • A readable rule DSL. Pipe rules together per field: "required|integer|range(1...10)". Arguments go in parentheses and are trimmed. See the Rules Reference.
  • 30+ built-in rules for types, text, contact info, sizes, dates, patterns and comparisons.
  • Callbacks and custom named rules. Drop in a closure for a one-off check, or register a reusable rule with extend().
  • Localized messages. Ships with English and Turkish; placeholders, labels and per-rule overrides are all supported. See Localization.
  • Fails loud, not silent. An unknown rule throws instead of quietly passing a field. See Exceptions.
  • Zero runtime dependencies beyond ext-mbstring.

At a glance — a few rules

Rule Passes Fails
integer "12", -3 "3.5", "abc"
mail a@b.com not-an-email
range(1970...2099) 2022 1900
length(3...20) "hello" "ab"
again(password) confirm equals password they differ
only(small, medium, large) "Medium" "xl"

Package metadata

If something in this wiki is unclear, wrong, or out of date, open an issue — documentation fixes are merged eagerly.

Clone this wiki locally