A simple validation class for PHP designed to validate various types of input data according to specified rules.
You can install the Lithe Validator module via Composer. Run the following command in your project directory:
composer require lithemod/validatorMake sure to include the Composer autoloader in your PHP script:
require 'vendor/autoload.php';Create an instance of the Validator class by passing the data and the validation rules you want to apply.
use Lithe\Base\Validator;
$data = [
'email' => 'example@example.com',
'name' => 'John Doe',
'age' => '25',
];
$rules = [
'email' => 'required|email',
'name' => 'required|name',
'age' => 'required|integer|min:18|max:65',
];
$validator = new Validator($data, $rules);You can check if all validation rules pass using the passed() method:
if ($validator->passed()) {
echo "Validation passed!";
} else {
echo "Validation failed!";
print_r($validator->errors());
}required: Checks if the field is present and not empty.
Error Code: 1001email: Validates the format of an email address.
Error Code: 1002url: Validates the format of a URL.
Error Code: 1003ip: Validates the format of an IP address.
Error Code: 1004number: Checks if the field is a numeric value.
Error Code: 1005integer: Checks if the field is an integer.
Error Code: 1006boolean: Validates if the field is a boolean value.
Error Code: 1007min: Validates the minimum length of the field value.
Error Code: 1009max: Validates the maximum length of the field value.
Error Code: 1010range: Checks if the field value is within a specified range.
Error Code: 1011dateFormat: Validates the date format.
Error Code: 1012alphanumeric: Checks if the field value is alphanumeric.
Error Code: 1013name: Validates that the field contains only letters and spaces.
Error Code: 1014in: Checks if the field value is one of the allowed values.
Error Code: 1015
$data = [
'email' => 'user@example.com',
'name' => 'Alice',
'age' => '30',
];
$rules = [
'email' => 'required|email',
'name' => 'required|name',
'age' => 'required|integer|min:18|max:65',
];
$validator = new Validator($data, $rules);
if ($validator->passed()) {
echo "All validations passed!";
} else {
echo "There were validation errors:";
print_r($validator->errors());
}This project is licensed under the MIT License - see the LICENSE file for details.