Download the PHP package iviphp/validation without Composer
On this page you can find all versions of the php package iviphp/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iviphp/validation
More information about iviphp/validation
Files in iviphp/validation
Package validation
Short Description Validation rules and data validation for the IviPHP ecosystem.
License MIT
Informations about the package validation
Ivi Validation
Validation rules and data validation for the IviPHP ecosystem.
Overview
iviphp/validation provides a small object-based validation system for IviPHP packages and applications.
It supports:
- reusable validation rule objects;
- nested fields through dot notation;
- required and nullable fields;
- validated-data extraction;
- multiple errors per field;
- optional early termination;
- structured validation results;
- validation exceptions.
The package is framework-independent and does not depend on HTTP, routing, controllers or global application state.
Installation
Requirements
- PHP 8.2 or later
- Composer
iviphp/support
Basic validation
Check the result:
Creating a validator
A validator can be created and reused.
Nested fields
Dot notation can validate nested input values.
Validated values preserve their nested structure.
Result:
Required fields
The Required rule rejects:
null;- empty strings;
- strings containing only whitespace;
- empty arrays.
The following values are accepted:
false;0;'0';- non-empty arrays;
- non-empty strings.
Nullable fields
When the value is null, Nullable causes the remaining rules to be skipped.
Empty strings are not treated as null.
This value still fails the Email rule.
Required takes precedence over Nullable.
A null value still fails because the field is required.
String validation
StringType accepts only real PHP strings.
It does not convert integers, booleans or other values automatically.
Integer validation
The string '12' does not pass IntegerType.
Use Numeric when numeric strings should be accepted.
Numeric validation
Numeric accepts:
- integers;
- finite floating-point values;
- valid numeric strings.
It rejects:
- booleans;
- empty strings;
- arrays;
- objects;
NaN;- infinite values.
Email validation
Email validation uses PHP's FILTER_VALIDATE_EMAIL.
Leading or trailing whitespace is rejected instead of silently removed.
Minimum length
MinLength supports strings and arrays.
Strings are measured with mb_strlen() when the Mbstring extension is available. Otherwise, byte length is used.
Maximum length
Arrays can also be limited:
Allowed values
Strict comparison is enabled by default.
The string '1' does not match the integer 1.
Non-strict comparison can be enabled explicitly:
Custom validation messages
Most built-in rules accept an optional custom message.
Supported placeholders depend on the rule.
Common placeholders include:
:field;:min;:max;:values.
Validation results
A validation operation returns a ValidationResult.
Return all errors:
Example:
Check one field:
Return all messages for one field:
Return the first message for one field:
Return the first message from the complete result:
Return every message as a flat list:
Return the total number of errors:
Validated data
Only fields declared in the validation rules are returned as validated data.
Result:
The undeclared admin value is excluded.
Retrieve one validated value:
Nested dot notation is supported:
Check whether validated data contains a key:
Fields that fail validation are excluded from validated data.
Optional fields
A field that is absent and does not use Required is skipped.
This validation passes because nickname is optional.
When the optional field exists, its rules are applied.
Stopping after the first error
By default, the validator collects every failure for each field.
Enable early termination per field:
A reusable validator can also be changed immutably.
Validation exceptions
A failed result can be wrapped in a ValidationException.
Catch the exception:
Available exception methods:
A ValidationException cannot be created from a successful result.
Custom rules
Custom validation rules implement RuleInterface.
Use the custom rule normally:
The complete input array is provided to each rule.
This enables cross-field validation.
Complete example
Available classes
RuleInterface
Validator
ValidationResult
ValidationException
Built-in rules
Required
Requires a non-empty value.
Nullable
Allows null and skips remaining rules.
StringType
Requires a PHP string.
IntegerType
Requires a PHP integer.
Numeric
Requires an integer, finite float or numeric string.
Email
Requires a valid email address.
MinLength
Requires a minimum string character count or array element count.
MaxLength
Requires a maximum string character count or array element count.
In
Requires a value to exist in an allowed list.
Design principles
- Object-based validation rules
- Explicit and reusable rule behavior
- Nested field support through dot notation
- No global validator state
- No automatic data coercion
- Strict value comparison by default
- Validated-data extraction
- Structured errors per field
- Framework-independent implementation
- Clear extension through custom rules
This package validates values but does not transform them automatically.
Data normalization, sanitization and type conversion should happen explicitly before or after validation depending on application requirements.
Package boundaries
This package is responsible for:
- validating input arrays;
- applying reusable rule objects;
- collecting validation errors;
- returning validated values;
- representing failed validation operations.
It is not responsible for:
- reading HTTP requests;
- generating HTTP responses;
- database uniqueness checks;
- authentication;
- authorization;
- form rendering;
- translating messages;
- global application state.
Database-aware and application-aware rules should be implemented in higher-level packages or application code.
Ecosystem
This package is part of the IviPHP ecosystem:
iviphp/contractsiviphp/supportiviphp/configiviphp/containeriviphp/httpiviphp/databaseiviphp/cacheiviphp/viewiviphp/authiviphp/framework
Contributing
Contributions should preserve the focused scope of this package.
New rules should:
- implement
RuleInterface; - avoid hidden data mutation;
- return deterministic results;
- provide clear validation messages;
- remain independent from the full framework;
- avoid global service access;
- avoid implicit database or network operations.
Security
Validation does not replace security controls such as:
- authorization;
- output escaping;
- SQL parameter binding;
- CSRF protection;
- upload scanning;
- rate limiting;
- password hashing.
Input should be validated as close as possible to the application boundary.
Please report security issues privately to the Softadastra maintainers instead of opening a public issue.
License
This project is licensed under the MIT License.
Maintainer
Created and maintained by Softadastra.