wijoc / validify-mi
A stand alone validation for PHP and Wordpress.
Requires
- php: ^7.4 || ^8.0 || ^8.1 || ^8.2
This package is not auto-updated.
Last update: 2025-08-12 02:51:58 UTC
README
Standalone PHP Validation that is compatible with procedural PHP and WordPress. This project provides a set of validation rules to simplify data validation in your projects, along with data sanitization specifically for WordPress projects. Heavily influenced by Laravel validation and rakit/validation, this project originated from a personal need and is still in beta development. All inputs are highly appreciated.
Table of Contents
Features
- Simple syntax
- Supports multiple validation rules
- Compatible with customs error messages
- Compatible with procedural PHP and WordPress
- Support Wordpress sanitazion
- Support nested validation
Installation
You can install the library via Composer:
composer require wijoc/validify-mi
Usage
Here is how you use the validation using ::make function. Validator::make has 4 arguments :
- Input that you want to validate.
- Rules of validation.
- Custom validation message.
- Sanitizion rules. (for now only available for wordpress project) All arguments should be an array.
<?php require 'vendor/autoload.php'; use Wijoc\ValidifyMI\Validator; $input = [ 'email' => 'user@example.com', 'age' => 25 ]; $rules = [ 'email' => ['required', 'email'], 'age' => ['required', 'numeric'] ]; $message = [ 'email.required' => "Email is required!", 'email.email' => "Email is invalid!", 'age.required' => "Age is required!", 'age.numeric' => "Age must be numeric!" ]; /** Create validator * * You can add sanitizer as 4th arguments * * (for now it's limited to wordpress sanitize) */ $validator = Validator::make($input, $rules, $message); if ($validator->fails()) { /** VaType Islidation failed */ print_r($validator->errors('all')); /** Get first error */ $validator->errors()->firstOfAll(); } else { /** Validation passed */ echo "Validation successful!"; /** get validated data */ $validated = $validator->validate(); }
Also you can validate nested input with :
<?php require 'vendor/autoload.php'; use Wijoc\ValidifyMI\Validator; $input = [ 'email' => 'user@example.com', 'age' => 25, 'phoneNumber' => [ '123456789', '987654321' ], 'socialMedia' => [ 'facebook' => 'https://facebook.com', 'twitter' => 'https://twitter.com' ], 'address' => [ [ 'city' => 'Jakarta', 'province' => 'DKI Jakarta', 'postalCode' => '123' ] ] ]; $rules = [ 'email' => ['required', 'email'], 'age' => ['required', 'numeric'], 'phoneNumber' => ['min:1'], 'phoneNumber.*' => ['numeric'], 'socialMedia.facebook' => ['required'], 'address.*.postalCode' => ['required', 'numeric'], ]; $message = [ 'email.required' => "Email is required!", 'email.email' => "Email is invalid!", 'age.required' => "Age is required!", 'age.numeric' => "Age must be numeric!" ]; /** Create validator * * You can add sanitizer as 4th arguments * * (for now it's limited to wordpress sanitize) */ $validator = Validator::make($input, $rules, $message); if ($validator->fails()) { /** Validation failed */ print_r($validator->errors('all')); } else { /** Validation passed */ echo "Validation successful!"; }
Explanation
First, we prepare all the necessary parameters to initialize the validator.
- Input to validate :
/** Input Value to validate */ $input = [ 'email' => 'user@example.com', 'age' => 25 ];
- Validation rules :
/** Validation Rule */ $rules = [ 'email' => ['required', 'email'], 'age' => ['required', 'numeric'] ];
- Custom validation message :
$message = [ 'email.required' => "Email is required!", 'email.email' => "Email is invalid!", 'age.required' => "Age is required!", 'age.numeric' => "Age must be numeric!" ];
Next, we initialize it with the Validator::make() function using the prepared arguments.
/** Create validator * * You can add sanitizer as 4th arguments * * (for now it's limited to wordpress sanitize) */ $validator = Validator::make($input, $rules, $message);
Afterwards, we can check if the input is validated or not using either the validate() or fails() function: validate() will return true if all input is validated, and false if input is not validated.
/** Using validate() function */ $validator->validate()
or On the other hand, fails() will return false if input is validated, and true if all input is not validated.
/** Using fails function */ $validator->fails()
Example :
if ($validator->fails()) { /** Validation failed */ print_r($validator->errors('all')); } else { /** Validation passed */ echo "Validation successful!"; }
If the data is not validated, we can retrieve all errors using the errors('all') function. The 'all' argument is required to get all errors.
if ($validator->fails()) { /** Validation failed */ print_r($validator->errors('all')); } else { /** Validation passed */ echo "Validation successful!"; }
Alternatively, if you only want to get the first error, you can use the firstOfAll() method on top of the errors() function. Please note: If you use firstOfAll(), you don't need to add the 'all' argument to the errors() function.
if ($validator->fails()) { /** Validation failed */ print_r($validator->errors()->firstOfAll()); // print_r($validator->errors('all')->firstOfAll()); -> This will result an exception } else { /** Validation passed */ echo "Validation successful!"; }
Finally, you can get your validated input with the validated() function. validated() function will return an array of input if all data is validated, or an empty array if the data is not validated.
if ($validator->fails()) { /** Validation failed */ print_r($validator->errors()->firstOfAll()); // print_r($validator->errors('all')->firstOfAll()); -> This will result an exception } else { /** Validation passed */ echo "Validation successful!"; /** get validated */ $validated = $validator->validated(); print_r($validated); }
Additionally, if you are working on a WordPress project, you can use the sanitization feature. Check here for the sanitization feature.
Validation Rules
-
Required
Check if given data is empty.
$rules = [ 'input' => ['required'] ];
-
Required If
Check if given data is empty, depends on another fields value.
$rules = [ 'input' => ['requiredif:fieldA,true'] ];
-
email
Check if given data is a valid email address, using PHP FILTER_VALIDATE_EMAIL
$rules = [ 'input' => ['email'] ];
-
url
Check if given data is a valid url, using php FILTER_VALIDATE_URL
$rules = [ 'input' => ['url'] ];
-
numeric
Check if given data is numeric.
$rules = [ 'input' => ['numeric'] ];
-
max
Check max element of array, or total character of string. A numeric value will treated as string. Use greater_than, greater_than_equal, or compare_number to validate numeric value.
$rules = [ 'input' => ['max:9'] ];
-
min
Check min element of array, or total character of string. A numeric value will treated as string. Use less_than, less_than_equal, or compare_number to validate numeric value.
$rules = [ 'input' => ['min:9'] ];
-
greater-than
Check value is greater than given parameter. Value must be numeric.
$rules = [ 'input' => ['greater_than:9'] ];
or
$rules = [ 'input' => ['gt:9'] ];
-
greater-than-equal
Check value is greater than or equal to given parameter. Value must be numeric.
$rules = [ 'input' => ['greater_than_equal:9'] ];
or
$rules = [ 'input' => ['gte:9'] ];
-
less-than
Check value is less than given parameter. Value must be numeric.
$rules = [ 'input' => ['less_than:9'] ];
or
$rules = [ 'input' => ['lt:9'] ];
-
less-than-equal
Check value is less than or equal to given parameter. Value must be numeric.
$rules = [ 'input' => ['less_than_equal:9'] ];
or
$rules = [ 'input' => ['lte:9'] ];
-
compare-number
Check value by compare with given parameter. Value must be numeric.
$rules = [ 'input' => ['compare_number:{operator},{parameter to compare}'] ];
example :
$rules = [ 'input' => ['compare_number:>,9'] ];
-
date
Check value is a date and in same format as parameter.
$rules = [ 'input' => ['date:{date format}'] ];
example :
$rules = [ 'input' => ['date:Y-m-d H:i:s'] ];
-
date-more-than
Check value is a date later than given parameter. Parameter can be other input.
$rules = [ 'input' => ['date_more_than:{date or request field},{date format}'] ];
example :
$rules = [ 'input' => ['date_more_than:2024-12-01 01:59:59, Y-m-d H:i:s'] ];
or :
$request = [ 'inputToCompare' => '2024-01-30', 'input' => '2024-12-31', ]; $rules = [ 'input' => ['date_more_than:inputToCompare, Y-m-d'] ];
-
date-less-than
Check value is a date older than given parameter. Parameter can be other input.
$rules = [ 'input' => ['date_less_than:{date or request field},{date format}'] ];
example :
$rules = [ 'input' => ['date_less_than:2024-12-01 01:59:59,Y-m-d H:i:s'] ];
or :
$request = [ 'inputToCompare' => '2024-01-30', 'input' => '2024-12-31', ]; $rules = [ 'input' => ['date_less_than:inputToCompare,Y-m-d'] ];
-
date-between
Check value is a date is between than given parameter. Parameter can be another request input.
$rules = [ 'input' => ['date_between:{start date or request field},{end date or request field},{date format}'] ];
example :
$rules = [ 'input' => ['date_between:2024-12-01 01:59:59,2024-12-31 01:59:59,Y-m-d H:i:s'] ];
or :
$request = [ 'inputStart' => '2024-01-01', 'inputEnd' => '2024-01-30', 'input' => '2024-12-31', ]; $rules = [ 'input' => ['date_between:inputStart,inputEnd,Y-m-d'] ];
-
exists
-
not-exists
-
match
Check value is a match/exactly same with given parameter. Parameter must be another request input.
$rule = [ 'input' => ['match:{request field}']; ];
Example :
$request = [ 'inputToCompare' => 'value to compare', 'input' => 'value input' ]; $rule = [ 'input' => ['match:inputToCompare']; ];
-
not-match
Check value is not match or not exactly same with given parameter. Parameter must be another request input.
$rule = [ 'input' => ['not_match:{request field}']; ];
Example :
$request = [ 'inputToCompare' => 'value to compare', 'input' => 'value input' ]; $rule = [ 'input' => ['not_match:inputToCompare']; ];
-
file
Check value is a files input.
$rule = [ 'input' => ['files']; ];
or
$rule = [ 'input' => ['file']; ];
or
$rule = [ 'input' => ['is_file']; ];
-
file-max-size
Check input file size is not less than given parameter.
$rule = [ 'input' => ['max_file_size:{size in KB}']; ];
Example :
$rule = [ 'input' => ['max_file_size:20']; ];
-
mime
Check input file mime is one of than given parameter. Parameter can be multiple splitted by " , " (comma)
$rule = [ 'input' => ['mime:{mime type}']; ];
Example :
$rule = [ 'input' => ['mime:image/png,image/jpeg,application/pdf']; ];
-
in
Check input value is one of than given parameter. Parameter can be multiple splitted by " , " (comma)
$rule = [ 'input' => ['in:value1,value2']; ];
-
regex
Check input value is match regex pattern.
$rule = [ 'input' => ['regex:^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])(?=\S+$).*']; ];
-
Type Is
Check if input value type is match the parameter. Available type to validate:
- string: string || str
- numeric: numeric || number
- boolean: bool || boolean
- array: array
$rule = [ /** Pick one of this usage */ 'input' => ['typeis:{type}']; 'input' => ['type_is:{type}']; 'input' => ['is:{type}']; ];
Example :
$rule = [ 'input' => ['is:str'], 'inputOne' => ['is:array'], 'inputOne.*' => ['is:string'], ];
Sanitisation
Sanitisation currently only works for WordPress projects, so it uses WordPress' default sanitizer functions. Usage :
<?php require 'vendor/autoload.php'; use Wijoc\ValidifyMI\Validator; $input = [ 'email' => 'user@example.com', 'age' => 25, 'phoneNumber' => [ '123456789', '987654321' ], 'socialMedia' => [ 'facebook' => 'https://facebook.com', 'twitter' => 'https://twitter.com' ], 'address' => [ [ 'city' => 'Jakarta', 'province' => 'DKI Jakarta', 'postalCode' => '123' ] ] ]; $rules = [ 'email' => ['required', 'email'], 'age' => ['required', 'numeric'], 'phoneNumber' => ['min:1'], 'phoneNumber.*' => ['numeric'], 'socialMedia.facebook' => ['required'], 'address.*.postalCode' => ['required', 'numeric'], ]; $message = [ 'email.required' => "Email is required!", 'email.email' => "Email is invalid!", 'age.required' => "Age is required!", 'age.numeric' => "Age must be numeric!" ]; $sanitizer = [ 'email' => 'email' 'age' => 'text' 'phoneNumber.*' => 'text', 'socialMedia.facebook' => 'text', 'address.*.province' => 'kses' ]; /** Create validator * * You can add sanitizer as 4th arguments * * (for now it's limited to wordpress sanitize) */ $validator = Validator::make($input, $rules, $message, $sanitizer); if ($validator->fails()) { /** Validation failed */ print_r($validator->errors('all')); } else { /** get validated data */ $validated = $validator->validated(); /** get sanitized data */ $sanitized = $validator->sanitized(); }
$sanitizer = [ 'input' => 'email' ];
$sanitizer = [ 'input' => 'textarea' ];
$sanitizer = [ 'input' => 'text' ];
$sanitizer = [ 'input' => 'kses' ];
$sanitizer = [ 'input' => 'ksespost' ];