Validator
The validator is a simple PHP validation library to remove the hassle of creating (often times) ugly validation logic and takes huge inspiration from Laravel's internal validation library., (*1)
This library is intended to be lightweight (no dependencies!) and easy-to-use., (*2)
, (*3)
Requirements:
* PHP 7.1 and higher, (*4)
To install, run the following in your project root:, (*5)
$ composer require grahamsutton/validator
Available Rules, (*6)
Two Ways to Use It
There are two ways to perform validations:, (*7)
Chain Methods, (*8)
You can chain validation methods together if you want to perform validations..., (*9)
use Validator\Validator;
$is_valid = (new Validator())
->required('name', 'Graham')
->numeric('age', 33)
->email('email', 'grahamsutton2@gmail.com')
->int('total', 123)
->float('amount', 123.45)
->boolean('likes_dinos', true)
->min('fav_color', 'green', 3)
->max('state', 'FL', 2)
->date('birthday', '1980-01-01')
->array('fav_nums', [7, 4, 92])
->accepted('terms', true)
->afterDate('start_date', '2018-05-24', '2018-01-31')
->beforeDate('end_date', '2024-05-24', '2218-01-31')
->validate();
-or-, (*10)
Pipe Format (preferred), (*11)
Use can use pipe formatting the same way you do in Laravel if you want to perform multiple validations on a field. This is the preferred method of performing validations and looks better most of the time., (*12)
use Validator\Validator;
$validator = new Validator([
'name' => 'required|min:3|max:15',
'age' => 'required|numeric',
'email' => 'required|email',
'total' => 'required|int',
'amount' => 'required|float'
'likes_dinos' => 'required|boolean',
'date' => 'required|date',
'fav_nums' => 'required|array',
'terms' => 'accepted',
'start_date' => 'required|afterDate:2018-01-31',
'end_date' => 'required|beforeDate:2218-01-31',
]);
$is_valid = $validator->validate([
'name' => 'someone',
'age' => 23,
'email' => 'someone@example.com',
'total' => 123,
'amount' => 123.45,
'likes_dinos' => true,
'date' => '2018-02-18 23:00:00',
'fav_nums' => [7, 4, 92],
'terms' => true,
'start_date' => '2018-05-24',
'end_date' => '2024-05-24',
]));
Retrieving Errors
Retrieving errors is simple. When a validation fails to pass, it will be recorded into an array that can be retrieved from two different methods: getErrors
or getAllErrors
., (*13)
getErrors: array, (*14)
This will return a flat associative array, where the key is the name of the failed field and the value is the first error detected., (*15)
Note: Notice how some values fail multiple validations, like name
, but there is only error message per field., (*16)
use Validator\Validator;
$validator = new Validator([
'name' => 'required|min:3|max:15',
'age' => 'required|numeric',
'email' => 'required|email',
'total' => 'required|int',
'amount' => 'required|float'
'likes_dinos' => 'required|boolean',
'date' => 'required|date',
'fav_nums' => 'required|array',
'terms' => 'accepted',
'start_date' => 'required|afterDate:2018-01-31',
'end_date' => 'required|beforeDate:2218-01-31',
]);
// $is_valid will be false
$is_valid = $validator->validate([
'name' => '', // invalid
'age' => 'string', // invalid
'email' => '@example.com', // invalid
'total' => 123.45, // invalid
'amount' => 123, // invalid
'likes_dinos' => 12, // invalid
'date' => 'incorrect' // invalid
'fav_nums' => 'should be an array', // invalid
'terms' => false, // invalid
'start_date' => '2017-05-24', // invalid
'end_date' => '2324-05-24', // invalid
]));
$validator->getErrors();
// Returns:
// [
// 'name' => 'The name field is required.',
// 'age' => 'The age field must be a numeric value.',
// 'email' => 'The email field must be a valid email.',
// 'total' => 'The total field must be an integer.',
// 'amount' => 'The amount field must be a float.',
// 'likes_dinos' => 'The likes_dinos field must be a boolean value.',
// 'date' => 'The date field is not a valid date.',
// 'fav_nums' => 'The fav_nums field must be an array.',
// 'terms' => 'The terms field must be accepted.',
// 'start_date' => 'The start_date field value must be after 2018-01-31 00:01:00 (yyyy-mm-dd hh:mm:ss).',
// 'end_date' => 'The end_date field value must be before 2218-01-31 00:01:00 (yyyy-mm-dd hh:mm:ss).',
// ]
getAllErrors: array, (*17)
If you want to get all error messages recorded for a all fields, just use getAllErrors
method. This will return the name of the failed fields as the keys and an array of error messages per failed field as the values., (*18)
use Validator\Validator;
$validator = new Validator([
'name' => 'required|min:3|max:15',
'age' => 'required|numeric',
'email' => 'required|email',
'accepted' => 'required|boolean',
'date' => 'required|date'
]);
// $is_valid will be false
$is_valid = $validator->validate([
'name' => '', // invalid
'age' => 'string', // invalid
'email' => '@example.com', // invalid
'accepted' => 12, // invalid
'date' => 'incorrect' // invalid
]));
$validator->getAllErrors();
// Returns:
// [
// 'name' => [
// 'The name field is required.',
// 'The name field must be greater than or equal to 3 characters.'
// ],
// 'age' => ['The age field must be a numeric value.'],
// 'email' => ['The email field must be a valid email.'],
// 'accepted' => ['The accepted field must be a boolean value.'],
// 'date' => ['The date field is not a valid date.']
// ]
Available Rules
, (*19)
required
, (*20)
Validates that value is not empty and the field is present., (*21)
use Validator\Validator;
$is_valid = (new Validator)
->required('field_name', 'value')
->validate();
or, (*22)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'required'
]);
$is_valid = $validator->validate([
'field_name' => 'value'
]);
Default error message: The {field_name} field is required.
, (*23)
, (*24)
max:int
, (*25)
Determines the max value or string length that a field can have., (*26)
If the value provided is an integer, the validation will compare that the provided integer is less than or equal to the specified int value., (*27)
If the value provided is a string, the validation will compare that the value's string length (determined by PHP's strlen function) is less than or equal to the specified int value., (*28)
use Validator\Validator;
$is_valid = (new Validator)
->max('field_name', 'value', $max = 8)
->validate();
or, (*29)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'max:8'
]);
$is_valid = $validator->validate([
'field_name' => 'value'
]);
Default error message: The {field_name} field must be less than or equal to {int} characters.
, (*30)
, (*31)
min:int
, (*32)
Determines the minimum value or string length that a field can have., (*33)
If the value provided is an integer, the validation will compare that the provided integer is greater than or equal to the specified int value., (*34)
If the value provided is a string, the validation will compare that the value's string length (determined by PHP's strlen function) is greater than or equal to the specified int value., (*35)
use Validator\Validator;
$is_valid = (new Validator)
->min('field_name', 'value', $min = 3)
->validate();
or, (*36)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'min:3'
]);
$is_valid = $validator->validate([
'field_name' => 'value'
]);
Default error message: The {field_name} field must be greater than or equal to {int} characters.
, (*37)
, (*38)
int
, (*39)
Determines that a value is an integer based on PHP's is_int
function., (*40)
use Validator\Validator;
$is_valid = (new Validator)
->int('field_name', $value = 123)
->validate();
or, (*41)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'int'
]);
$is_valid = $validator->validate([
'field_name' => 123
]);
Default error message: The {field_name} field must be an integer.
, (*42)
, (*43)
float
, (*44)
Determines that a value is a float based on PHP's is_float
function., (*45)
use Validator\Validator;
$is_valid = (new Validator)
->float('field_name', $value = 123.45)
->validate();
or, (*46)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'float'
]);
$is_valid = $validator->validate([
'field_name' => 123.45
]);
Default error message: The {field_name} field must be an float.
, (*47)
, (*48)
numeric
, (*49)
Determines that a value is numeric based on PHP's is_numeric
function. The value can be a string, as long as it can be cast to a numerical value., (*50)
use Validator\Validator;
$is_valid = (new Validator)
->numeric('field_name', $value = 23)
->validate();
or, (*51)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'numeric'
]);
$is_valid = $validator->validate([
'field_name' => 23
]);
Default error message: The {field_name} field must be a numeric value.
, (*52)
, (*53)
email
, (*54)
Determines that a value is a valid email address., (*55)
use Validator\Validator;
$is_valid = (new Validator)
->email('field_name', 'value@example.com')
->validate();
or, (*56)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'email'
]);
$is_valid = $validator->validate([
'field_name' => 'value@example.com'
]);
Default error message: The {field_name} field must be a valid email.
, (*57)
, (*58)
boolean
, (*59)
Determines that a value is true
, false
, 0
, 1
, "0"
, or "1"
., (*60)
use Validator\Validator;
$is_valid = (new Validator)
->boolean('field_name', $value = true)
->validate();
or, (*61)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'boolean'
]);
$is_valid = $validator->validate([
'field_name' => true
]);
Default error message: The {field_name} field must be a boolean value.
, (*62)
, (*63)
accepted
, (*64)
Determines that a value is true
, 1
, "1"
, or "yes"
. This is useful for ensuring that people accept things like terms and conditions for your site. This validation will fail if provided a false value., (*65)
"yes"
will be parsed to lowercase when provided, just in case you provide it capitalized., (*66)
use Validator\Validator;
$is_valid = (new Validator)
->accepted('field_name', $value = true)
->validate();
or, (*67)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'accepted'
]);
$is_valid = $validator->validate([
'field_name' => true
]);
Default error message: The {field_name} field must be accepted.
, (*68)
, (*69)
array
, (*70)
Determines that a value is an array. Internally, it uses PHP's is_array
function to determine its truthiness. Empty arrays are considered valid. Add a required
validation to disallow empty arrays., (*71)
use Validator\Validator;
$is_valid = (new Validator)
->array('field_name', $value = ['value1', 'value2'])
->validate();
or, (*72)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'array'
]);
$is_valid = $validator->validate([
'field_name' => ['value1', 'value2']
]);
Default error message: The {field_name} field must be an array.
, (*73)
, (*74)
date
, (*75)
Determines that a value can be parsed by PHP's strtotime
function., (*76)
use Validator\Validator;
$is_valid = (new Validator)
->date('field_name', $value = '2018-07-24 03:30:24')
->validate();
or, (*77)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'date'
]);
$is_valid = $validator->validate([
'field_name' => '2018-07-24 03:30:24'
]);
Default error message: The {field_name} field is not a valid date.
, (*78)
, (*79)
afterDate:string
, (*80)
Determines if a value is after the specified date. The string parameter can be any value that can be parsed by PHP's strtotime
function., (*81)
use Validator\Validator;
$is_valid = (new Validator)
->afterDate('field_name', $value = '2018-07-25 03:30:24', $after_date = '2018-07-24 00:00:00')
->validate();
or, (*82)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'afterDate:2018-07-24 00:00:00'
]);
$is_valid = $validator->validate([
'field_name' => '2018-07-25 03:30:24'
]);
Default error message: The {field_name} field value must be after {after_date} (yyyy-mm-dd hh:mm:ss).
, (*83)
, (*84)
beforeDate:string
, (*85)
Determines if a value is before the specified date. The string parameter can be any value that can be parsed by PHP's strtotime
function., (*86)
use Validator\Validator;
$is_valid = (new Validator)
->beforeDate('field_name', $value = '2018-07-24 03:30:24', $before_date = '2018-07-25 00:00:00')
->validate();
or, (*87)
use Validator\Validator;
$validator = new Validator([
'field_name' => 'beforeDate:2018-07-25 00:00:00'
]);
$is_valid = $validator->validate([
'field_name' => '2018-07-24 03:30:24'
]);
Default error message: The {field_name} field value must be before {before_date} (yyyy-mm-dd hh:mm:ss).
, (*88)
License
Validator is licensed under the MIT License - see the LICENSE file for details., (*89)