-
Notifications
You must be signed in to change notification settings - Fork 1
API Reference
Everything lives on InitPHP\Validation\Validation. Every setter returns
$this for chaining unless noted otherwise.
use InitPHP\Validation\Validation;-
Data:
__construct·setData·mergeData·getData -
Rules:
rule·extend·pattern -
Running:
validation·isValid·clear -
Errors:
getError·setError -
Localization:
setLocale·setLocaleDir·setLocaleArray·labels -
Misc:
version·VERSION
public function __construct(array $data = [])Create a validator with an optional initial data set.
public function setData(array $data = []): selfReplace the entire data set.
public function mergeData(array $data = []): selfMerge values into the current data set. Existing keys are overwritten.
public function getData(): arrayReturn the current data set.
public function rule(string|array $key, string|callable|array $rule, ?string $err = null): selfQueue one or more rules for one or more fields.
-
$key— a field name, a pipe-separated list ("a|b"), or an array of names. -
$rule— a pipe-separated DSL string, a callable, or an array mixing both. The pseudo-ruleoptionalmarks the field(s) as allowed to be absent. -
$err— an optional custom message template for these rules.
Throws InvalidArgumentException if a key
or rule entry has an unsupported type.
public function extend(string $name, callable $callback, ?string $message = null): selfRegister a custom named rule usable from the DSL string. The callback receives the value followed by any rule arguments and returns a boolean. Registered rules persist across runs. See Callable & Custom Rules.
public function pattern(string $name, string $pattern = '[\w]+'): selfRegister or override a named pattern for the regex
rule. Names are case-insensitive; the body has no delimiters and is matched as
^(...)$. See Patterns & Regex.
public function validation(): boolEvaluate every queued rule. Clears the error list, runs each rule (skipping
optional absent fields), then empties the rule and optional
queues. Returns true when nothing failed.
Throws UndefinedRuleException if a string
rule names a rule that is neither built-in nor registered with extend().
public function isValid(): boolReturn whether the most recent validation() run had no errors, without
re-running.
public function clear(): selfDrop queued rules, optional flags and errors. Registered rules, patterns and the loaded locale are kept.
public function getError(): arrayReturn the list of messages from the most recent validation() run.
public function setError(string $error, array $context = []): selfAppend an interpolated message to the current error list. Note validation()
resets the list at the start of each run.
public function setLocale(string $locale = 'en'): selfLoad <locale>.php from the locale directory and merge it over the active
messages. Throws LocaleException if the file is
missing or does not return an array.
public function setLocaleDir(string $dir = __DIR__ . '/languages/'): selfSet the directory language files are loaded from. Throws
LocaleException if the directory does not exist.
public function setLocaleArray(array $assoc): selfMerge a partial set of message templates over the active locale.
public function labels(array $assoc): selfRegister display labels that replace raw field names in messages.
public function version(): stringReturn the library version.
public const VERSION = '2.0.0';The library version as a constant.
- Exceptions — the types thrown above.
-
Rules Reference — what goes inside
rule().
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other