olegkishko / php-field-validator
Lightweight, extensible validation library with chainable rules.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/olegkishko/php-field-validator
Requires
- php: ^8.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.1
README
A flexible and extensible PHP validation library that allows you to create chain validation rules for your data fields.
Features
- Chain multiple validation rules
- Built-in validation rules
- Custom validation rules support
- Callback-based validation
- Easy to extend and implement
Installation
composer require olegkishko/php-field-validator
Basic Usage
use OlegKishko\PhpFieldValidator\Validator; $validator = new Validator(); $validator->field('first_name')->required()->maxLength(250); $validator->field('last_name')->required()->maxLength(250); $validator->field('email')->required()->email()->maxLength(250); $validator->field('age')->integer()->between(14, 100); $validator->field('gender')->in('M', 'F'); $validator->field('linkedin')->url()->maxLength(250); $input = [ 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'john.doe@example.com', 'age' => 35, 'gender' => 'M', 'linkedin' => '', ]; $validator->validate($input); if ($validator->hasErrors()) { var_dump($validator->getErrors()); }
Built-in Rules
array()- Validates if value is an arraybetween(int $min, int $max)- Validates if numeric value is between specified rangeboolean()- Validates if value is a booleancontains(string $needle)- Validates if value contains a substringcount(int $count)- Validates if value is an array and consists of the specified number of elementsdistinct()- Validates if value is an array and all elements are uniqueeach(array $rules)- Validates if value is an array and each element is valid, according to the rulesemail()- Validates email formatendsWith(string $suffix)- Validates if value ends with a suffixequals(string|int|float|array $to)- Validates if value equals to passed valuefalse()- Validates if value is a boolean FALSEfloat()- Validates if value is a floatin(array $choices)- Validates if value is in passed choicesinteger()- Validates if value is an integerip(string $version)- Validates if value is an IP (any, ipv4 only, ipv6 only)length(int $length)- Validates if value is a string with the specified lengthmaxCount(int $maxCount)- Validates if value is an array and contains no more the specified number of elementsmaxLength(int $length)- Validates if value is a string and its length is not more than the specified lengthmax(int|float $max)- Validates if value is a numeric and not more than the specified valueminCount(int $minCount)- Validates if value is an array and contains at least the specified number of elementsminLength(int $length)- Validates if value is a string and its length is at least the specified lengthmin(int|float $min)- Validates if value is a numeric and at least the specified valueinteger()- Validates if value is a negative numericnotBlank()- Validates if value is not emptynotEquals(string|int|float|array $to)- Validates if value not equals to passed valuenotIn(array $choices)- Validates if value is not in passed choicesnumeric()- Validates if value is numericpositive()- Validates if value is a positive numericregex(string $pattern)- Validates value against a custom regex patternrequired()- Validates if value is not emptystartsWith(string $prefix)- Validates if value starts with a prefixstring()- Validates if value is a stringtrue()- Validates if value is a boolean TRUEurl()- Validates URL format
Chaining Rules
You can chain multiple rules for complex validation scenarios:
$validator ->field('first_name') ->required() ->minLength(3) ->maxLength(20);
Callback Rules
Use callback rules for custom validation logic:
$validator ->field('password') ->required() ->callback( function (string $value): bool { return preg_match('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/', $value); }, 'Password must contain at least one letter and one number', );
Custom Rules
You can create custom validation rules by implementing the RuleInterface:
use OlegKishko\PhpFieldValidator\Rules\RuleInterface; class CustomPasswordRule implements RuleInterface { public function validate(mixed $value): bool { // Your custom validation logic return preg_match('/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/', $value); } public function getErrorMessage(): string { return 'Password must contain at least one letter, one number, and one special character'; } } // Usage $validator ->field('password') ->customRule(new CustomPasswordRule()); $validator->validate(['password' => 'simplepassword!']);
Error Messages
When validation hasErrors, you can retrieve error messages:
$validator->validate($input); if ($validator->hasErrors()) { foreach ($validator->getErrors() as $field => $error) { echo sprintf('[%s]: %s%s', $field, $error, PHP_EOL); } }
License
This project is licensed under the MIT License — see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.