ongom / php-validator
A PHP library for validating input
README
A flexible PHP validation class for validating strings, numbers, and files. It supports both static chaining and method chaining for clear and readable validation rules.
โ Available Static Methods
the name spce id use PHPValidator\Validate
Validate::string($value, $fieldName)
-Initialize validation for a string.
Validate::number($value, $fieldName)
-Initialize validation for a number (int or float).
Validate::file($key, $source = null, $fieldName = 'File')
-Initialize validation for an uploaded file. $key
is the name of the $_FILES array
. $source
can be FILES
or a custom file array (e.g. from an API).
๐ค String Validation Methods
Validate::string($value, $fieldName)
Method | Description |
---|---|
->min_length($len) |
Requires a minimum number of characters. |
->max_length($len) |
Requires a maximum number of characters. |
->uppercase() |
Requires at least one uppercase character. |
->lowercase() |
Requires at least one lowercase character. |
->has_number() |
Requires at least one numeric digit. |
->special_character() |
Requires at least one special character (non-alphanumeric). |
->email() |
Checks if the string is a valid email address. |
->url() |
Checks if the string is a valid URL. |
๐ข Number Validation Methods
Method | Description |
---|---|
->min($value) |
Minimum numeric value allowed. |
->max($value) |
Maximum numeric value allowed. |
->integer() |
Must be an integer. |
->float() |
Must be a float (or an integer is also accepted). |
๐๏ธ File Validation Methods
Use after Validate::file('input_name')
.
Method | Description |
---|---|
->min_size($kb) |
Minimum size in kilobytes (KB). |
->max_size($kb) |
Maximum size in kilobytes (KB). |
->allowed_extension($exts) |
Allowed file extensions (string or array). |
->allowed_mime($mimes) |
Allowed MIME types (string or array). |
๐ง Custom Validation
->custom(callable $callback, string $errorMessage)
Run any custom logic. If it fails, the provided error message is returned.
Example:
->custom(fn($v) => strlen($v) % 2 === 0, 'Length must be even.')
๐ฆ Final Step
->run()
Executes all validation rules and returns an array of error messages.
๐งช Examples
Validate String
$errors = Validate::string('Hello123!') ->min_length(5) ->max_length(10) ->uppercase() ->lowercase() ->has_number() ->special_character() ->run();
Validate Number
$errors = Validate::number(5.5) ->min(0) ->max(10) ->float() ->run();
Validate File Upload
$errors = Validate::file('image') ->min_size(100) // 100KB ->max_size(2048) // 2MB ->allowed_extension(['jpg', 'png']) ->allowed_mime(['image/jpeg', 'image/png']) ->run();
๐งผ Notes
run()
always returns an array. If valid, it will be an empty array.
File size is automatically handled in kilobytes.
You can extend this class easily with more rules or by subclassing.
Let me know if you'd like a version of this in a .md file or integrated into a documentation tool.