svil4ok / validation
PHP validation library
Requires
- php: ^7.0
Requires (Dev)
- phpunit/phpunit: 5.7.*
- squizlabs/php_codesniffer: 2.*
This package is not auto-updated.
Last update: 2025-05-02 22:25:12 UTC
README
Simple and lightweight standalone PHP library for validating data.
Usage
<?php use SGP\Validation\Validator; $input = [ 'name' => 'Svilen Popov', 'comment' => 'This a comment', 'users' => [ [ 'age' => 20, 'username' => 'username1' ], [ 'age' => 16, 'username' => 'username2' ] ] ]; $rules = [ 'name' => 'required', 'comment' => 'required|min:20', 'users.*.age' => 'required|min:18' ]; $messages = [ 'comment.min' => 'Your comment should have at least 20 chars.' ]; $validator = Validator::make($input, $rules, $messages); $errors = $validator->errors()->all(); foreach ($errors as $error) { echo $error . PHP_EOL; }
and the above example will output:
Your comment should have at least 20 chars.
The users.1.age minimum is 18
Available rules
Alpha | Integer |
---|---|
Alpha-Numeric | Max |
Boolean | Min |
Date after | Numeric |
Date before | Required |
Date |
Checks if the value contains only alphabetic characters.
'field' => 'alpha'- alpha_num
Checks if the valuecontains only alpha-numeric characters.
'field' => 'alpha_num'- boolean
Checks if the value is able to be cast as a boolean. Accepted input are true
, false
, 1
, 0
, "1"
, and "0"
.
'field' => 'boolean'- date_after:param
Checks if the value is a valid date and is after a given date.
'field' => 'date_after:2017-01-01'- date_before:param
Checks if the value is a valid date and is before a given date.
'field' => 'date_before:2017-01-01'- date
Checks if the value is a date/time string in the input format.
'field' => 'date'- max:int
Checks if the value is an integer.
'field' => 'int'- max:param
Checks if the value is less than given size. The rule works for strings, numerics and arrays.
'field' => 'max:20'- min:param
Checks if the value is greater than given size. The rule works for strings, numerics and arrays.
'field' => 'max:20'- max:numeric
Checks if the value is numeric.
'field' => 'numeric'- required
Checks if the value is not empty or null.
'field' => 'required'