hstanleycrow/easyphpformvalidator
Composer 安装命令:
composer require hstanleycrow/easyphpformvalidator
包简介
Lightweight PHP library for validating form input with a fluent, Laravel-like rule syntax.
README 文档
README
English | Español
EasyPHPFormValidator
Lightweight PHP library for validating form input, with a fluent, Laravel-like rule syntax.
Requirements
- PHP 8.2 or higher
- Composer
The library has no runtime dependencies beyond PHP itself.
Installation
composer require hstanleycrow/easyphpformvalidator
Quick example
use hstanleycrow\EasyPHPFormValidator\Validator; use hstanleycrow\EasyPHPFormValidator\ValidationException; require 'vendor/autoload.php'; $data = [ 'name' => 'Harold', 'email' => 'jeloumyfriend@gmail.com', 'age' => '46', ]; $rules = [ 'name' => 'required|string|min:3|max:10', 'email' => 'required|email', 'age' => 'required|integer|min:18', ]; try { Validator::validate($data, $rules); echo 'Validation passed!'; } catch (ValidationException $e) { foreach ($e->getErrors() as $field => $errors) { foreach ($errors as $error) { echo "$error\n"; } } }
Rules for the same field are chained with |. Rules that take a parameter use
rule:value (e.g. min:3, in:apple,banana,orange).
Custom error messages
Pass a third $messages array keyed as field.rule:
Validator::validate($data, $rules, [ 'name.required' => 'The name field is required', 'email.email' => 'You must provide a valid email address', ]);
Available rules
| Rule | Description |
|---|---|
required |
Value must be present and not an empty string. |
string |
Value must be a string. |
integer |
Value must be an integer, or a numeric string representing one. |
min:value |
Minimum numeric value if the input is numeric (including numeric strings like '46'), otherwise minimum string length. |
max:value |
Maximum numeric value if the input is numeric (including numeric strings like '46'), otherwise maximum string length. |
in:a,b,c |
Value must be one of the given comma-separated options. |
email |
Value must be a valid email address. |
url |
Value must be a valid http/https URL. |
phone |
Value must be a phone number (6 to 15 digits, optional leading +). |
confirmed:field |
Value must match field_confirmation in the same data array. |
decimal:places |
Value must be numeric with either no decimal part or exactly places decimal digits. |
decimalNumber |
Value must be numeric and not a whole number. |
greaterThanZero |
Value must be numeric and greater than zero. |
nullable |
Always passes; marks a field as optional. |
date:format |
Value must be a valid date in format (default Y-m-d). |
after:date |
Value must be a date strictly after date. |
afterOrEqual:date |
Value must be a date on or after date. |
before:date |
Value must be a date strictly before date. |
beforeOrEqual:date |
Value must be a date on or before date. |
fileExtension:a,b,c |
Value's file extension must be one of the given options. |
svphone:fix|mov |
Example custom rule: validates Salvadoran landline (fix) or mobile (mov) numbers. |
Extending with custom rules
Register a custom rule at runtime with Validator::extend(), without touching
the library's source:
use hstanleycrow\EasyPHPFormValidator\Rules\RuleInterface; use hstanleycrow\EasyPHPFormValidator\Validator; class EvenNumberRule implements RuleInterface { public function passes(mixed $value, array $data = []): bool { return is_numeric($value) && ((int) $value) % 2 === 0; } public function message(string $attribute): string { return "$attribute must be an even number."; } } Validator::extend('evenNumber', EvenNumberRule::class); Validator::validate(['quantity' => 3], ['quantity' => 'evenNumber']); // throws ValidationException: "quantity must be an even number."
Any rule that needs to compare against another field (like confirmed)
receives the full submitted data array as the second argument to passes().
Public methods
Validator
| Method | Description |
|---|---|
Validator::validate(array $data, array $rules, array $messages = []): void |
Validates $data against $rules. Throws ValidationException if any rule fails. |
Validator::extend(string $ruleName, string $ruleClass): void |
Registers a custom rule class (must implement RuleInterface) under $ruleName. |
ValidationException
| Method | Description |
|---|---|
getErrors(): array |
Returns errors as [field => [message, ...]]. |
RuleInterface
| Method | Description |
|---|---|
passes(mixed $value, array $data = []): bool |
Returns whether $value satisfies the rule. $data is the full submitted data array. |
message(string $attribute): string |
Returns the default error message for $attribute. |
Tests
composer test
See tests/ for the cases covered (happy path and errors) per rule.
AI assistant documentation
See AI_USAGE.md.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-09