Download the PHP package grahamsutton/validator without Composer
On this page you can find all versions of the php package grahamsutton/validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package validator
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.
This library is intended to be lightweight (no dependencies!) and easy-to-use.
Requirements:
- PHP 7.1 and higher
To install, run the following in your project root:
Available Rules
- required
- max
- min
- int
- float
- numeric
- boolean
- accepted
- array
- date
- afterDate
- beforeDate
Two Ways to Use It
There are two ways to perform validations:
Chain Methods
You can chain validation methods together if you want to perform validations...
-or-
Pipe Format (preferred)
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.
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
.
getErrors: array
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.
Note: Notice how some values fail multiple validations, like name
, but there is only error message per field.
getAllErrors: array
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.
Available Rules
required
Validates that value is not empty and the field is present.
or
Default error message: The {field_name} field is required.
max:int
Determines the max value or string length that a field can have.
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.
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.
or
Default error message: The {field_name} field must be less than or equal to {int} characters.
min:int
Determines the minimum value or string length that a field can have.
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.
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.
or
Default error message: The {field_name} field must be greater than or equal to {int} characters.
int
Determines that a value is an integer based on PHP's is_int
function.
or
Default error message: The {field_name} field must be an integer.
float
Determines that a value is a float based on PHP's is_float
function.
or
Default error message: The {field_name} field must be an float.
numeric
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.
or
Default error message: The {field_name} field must be a numeric value.
Determines that a value is a valid email address.
or
Default error message: The {field_name} field must be a valid email.
boolean
Determines that a value is true
, false
, 0
, 1
, "0"
, or "1"
.
or
Default error message: The {field_name} field must be a boolean value.
accepted
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.
"yes"
will be parsed to lowercase when provided, just in case you provide it capitalized.
or
Default error message: The {field_name} field must be accepted.
array
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.
or
Default error message: The {field_name} field must be an array.
date
Determines that a value can be parsed by PHP's strtotime
function.
or
Default error message: The {field_name} field is not a valid date.
afterDate:string
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.
or
Default error message: The {field_name} field value must be after {after_date} (yyyy-mm-dd hh:mm:ss).
beforeDate:string
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.
or
Default error message: The {field_name} field value must be before {before_date} (yyyy-mm-dd hh:mm:ss).
License
Validator is licensed under the MIT License - see the LICENSE file for details.