mediadevs / validator
A lightweight end extendable PHP validation package.
Requires
- php: ^7.2
- ext-curl: *
- ext-fileinfo: *
- ext-json: *
- ext-mysqli: *
- mediadevs/configuration: 1.0.1
- mediadevs/response-handler: 1.0.0
Requires (Dev)
- mikevandiepen/typhp: ^0.2.2
- nunomaduro/phpinsights: ^1.11.1
- php-coveralls/php-coveralls: ^2.0
- phpstan/phpstan: ^0.11.0
- phpunit/phpunit: 8
- sempro/phpunit-pretty-print: ^1.2.1
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2025-03-23 21:50:07 UTC
README
Install
Via Composer
$ composer require mediadevs/validator
Via GIT
HTTPS: git clone https://github.com/mediadevs/validator.git SSH: git clone git@github.com:mediadevs/validator.git
Usage
<?php Mediadevs\Validator\Validator::validate($clean, [ 'name' => 'required|string|min_length:6', 'email' => 'required|email|allowed_providers:hotmail.com,outlook.com,live.com,gmail.com,yourwebsite.tld', ], $language); /** * @var $language - is optional and defaults to english * @path './config/translations.php' */
Validation filters
Basic
Filter | Aliases | Functionality | Arguments | Category |
---|---|---|---|---|
equal |
equals equal_to equals_to |
Whether the value of the field equals the value of the threshold. | true |
Basic |
not_equal |
not_equal_to |
Whether the value of the field does not equal the value of the threshold. | true |
Basic |
regular_expression |
regex expression |
Whether the value of the field matches the regular expression threshold pattern. | true |
Basic |
required |
req |
Whether the field is set and is not empty. | false |
Basic |
array |
arr |
Whether the value of the field is an array. | false |
Basic |
boolean |
bool |
Whether the value of the field is a boolean. | false |
Basic |
float |
Whether the value of the field is a float. | false |
Basic | |
integer |
int |
Whether the value of the field is an integer. | false |
Basic |
null |
Whether the value of the field is null**. | false |
Basic | |
numeric |
num |
Whether the value of the field is numeric. | false |
Basic |
string |
str |
Whether the value of the field is a string. | false |
Basic |
Date
Filter | Aliases | Functionality | Arguments | Category |
---|---|---|---|---|
date_after |
after_date pre_date |
Whether the value of the field is after the threshold date. | true |
Date |
date_before |
before_date post_date |
Whether the value of the field is before the threshold date. | true |
Date |
date_between |
between_dates |
Whether the value of the field between the two threshold dates. | true |
Date |
Filter | Aliases | Functionality | Arguments | Category |
---|---|---|---|---|
allowed_email_providers |
allowed_providers email_allowed provider_allowed email_provider_allowed email_whitelist |
Whether the domain of the email address is from an allowed / whitelisted provider. | true |
|
blocked_email_providers |
blocked_providers email_blocked provider_blocked email_provider_blocked email_blacklist |
Whether the domain of the email address is from an blocked / blacklisted provider. | true |
|
email |
valid_email email_valid |
Whether the value of the field is an email. | false |
|
email_provider_exist |
provider_exists |
Whether the provider of the email exists and is a reachable host. | false |
Host
Filter | Aliases | Functionality | Arguments | Category |
---|---|---|---|---|
domain |
Whether the value of the field is a domain name. | false |
Host | |
ip |
ip_address |
Whether the value of the field is an IP address. | false |
Host |
ipv4 |
ipv4_address |
Whether the value of the field is an IPv4 address. | false |
Host |
ipv6 |
ipv6_address |
Whether the value of the field is an IPv6 address. | false |
Host |
mac |
mac_address |
Whether the value of the field is an MAC address | false |
Host |
reachable_address |
website_live test_host |
Whether the entered domain name / ip address is reachable. | false |
Host |
url |
Whether the value of the field is an url. | false |
Host |
Numeric
Filter | Aliases | Functionality | Arguments | Category |
---|---|---|---|---|
between |
numeric_between num_between |
Whether the numeric value is between the two numeric threshold values. The order of the numeric threshold values is not important. | true |
Numeric |
greater_than |
gt |
Whether the value of the field is greater than the threshold. | true |
Numeric |
greater_than_or_equal_to |
gte |
Whether the value of the field is greater or equal to the threshold. | true |
Numeric |
less_than |
lt |
Whether the value of the field is lesser than the threshold. | true |
Numeric |
less_than_or_equal_to |
lte |
Whether the value of the field is lesser or equal to the threshold. | true |
Numeric |
maximum |
max |
Whether the value of the field has less than the maximum value of the threshold. | true |
Numeric |
minimum |
min |
Whether the value of the field has the minimum value of the threshold. | true |
Numeric |
String
Filter | Aliases | Functionality | Arguments | Category |
---|---|---|---|---|
contains |
Whether the value of the field contains the threshold substring. | true |
String | |
ends_with |
ends |
Whether the value of the field ends with the threshold substring. | true |
String |
exact_length |
exact |
Whether the value of the field matches the exact threshold length. | true |
String |
maximum_length |
max_length maxlen |
Whether the string length of the field has less characters then the maximal required threshold length. | true |
String |
minimum_length |
min_length minlen |
Whether the string length of the field has the minimal required threshold length. | true |
String |
starts_with |
starts |
Whether the value of the field starts with the threshold substring. | true |
String |
Create your own validation filter
Creating a custom validation filter is not difficult, I have tried to make it as simple and streamlined as possible. This library is also structured to have each filter as a standalone class, all the classes will be registered in the service provider. Every filter can have an infinite amount of aliases (as long as the alias or the identifier is unique).
Register from outside the library
<?php Mediadevs\Validator\Services\FilterProvider::getInstance(); // Registering one of the test classes Mediadevs\Validator\Services\FilterProvider::register([ \Vendor\Package\Namespace\CustomFilters\MyFirstCustomFilter::class, \Vendor\Package\Namespace\CustomFilters\MySecondCustomFitler::class, ]);
Register from within the library
Step one
Create own validation or sanitization class.
- Navigate to
src/Filters/
and create a new folder named/Custom
Step two
- Create a new class (You can take the base class example from:
/examples/create_custom_filters/option_b/FilterExample.php
)
Step three
- Enter your custom validation logic, make sure it returns a
boolean
Step four
- Add your filters as a new item inside the $providers array inside
/src/Services/FilterProvider.php
<?php namespace Mediadevs\Validator\Services; class FilterProvider { /** * The filters / providers for the Validator package * The classes are grouped by validation type and sorted alphabetically. * * @var array */ private $providers = array( /*----[ Custom validation ]-----------------------------------------------------------------------------------*/ \Vendor\Package\Namespace\CustomFilters\MyFirstCustomFilter::class, \Vendor\Package\Namespace\CustomFilters\MySecondCustomFilter::class, ); }
Step five
- When you create a validation filter it is important that you add a response message which should be prompted to the user.
- For the response messages we use the package
mediadevs/response-handler
on versionv1.0.0
. - For the response message you can also add a translation per language. You can check the file for more information. It is not that exiting =)
Step six
Congratulations, you made your own filter! Now publish it to the world to use it!!
TODO:
- New validation filters
- credit_card
- iban
- address (Location)
- datetime format / pattern
- Project changes
- Add a method where you can manually overwrite response translations
- Create a facade with a different and lightweight approach for alternative validation
- Translations
- Swedish translation
- German translation
- General
- Make code more accessible for older versions of PHP
- Work on implementation for several library's (Symphony, Laravel, Yii, CackePHP, etc..)
Inspiration
This library took some heavy inspiration from some other libraries, I just wanted an easier and more
structured way to validate my $_POST[]
. Since some other library's / packages weren't that structured and just stored
all the filters in one file I decided to build my own.
Special thanks to:
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email contact@mediadevs.nl instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.