Download the PHP package guyliangilsing/php-validation without Composer
On this page you can find all versions of the php package guyliangilsing/php-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download guyliangilsing/php-validation
More information about guyliangilsing/php-validation
Files in guyliangilsing/php-validation
Package php-validation
Short Description A simple validation library that allows you to write custom validators for your data.
License MIT
Informations about the package php-validation
PHPValidation
A simple validation library that allows you to write custom validators for your data.
Table of Contents
- PHPValidation
- Table of Contents
- Features
- Installation
- Usage
- Obtaining a validator
- Through a factory
- Configuring array field validation
- Configuring custom error messages
- Using the validator
- Creating custom field validators
- Obtaining a validator
- Available field validators
- required
- notEmpty
- isArray
- hasKeys
- hasValues
- confirmValues
- in
- notIn
- minLength
- maxLength
- minCount
- maxCount
- phoneNumber
- isAlphabetic
- isNumeric
- isAlphaNumeric
- isInt
- isFloat
- isString
- isObject
- objectOfType
- equals
- contains
- greaterThan
- greaterEqual
- lowerThan
- lowerEqual
- between
- isDate
- dateHasFormat
- dateEquals
- dateLowerThan
- dateLowerEqual
- dateGreaterThan
- dateGreaterEqual
- dateBetween
Features
PHPValidation comes with the following features:
- Array validation through a simple and clean interface
- Custom field validator support
- Built-in field validators for basic use-cases
required
field validatornotEmpty
field validator for strings and arraysisArray
field validatorhasKeys
field validator for arrayshasValues
field validator for arraysconfirmValues
field validator for arraysin
field validator for strings, floats, integers, booleans, and arrays with the previous types inside of themnotIn
field validator for strings, floats, integers, and booleansminLength
field validator for stringsmaxLength
field validator for stringsminCount
field validator for arraysmaxCount
field validator for arraysemail
field validator for stringsphoneNumber
field validator for stringsisAlphabetic
field validator for stringsisNumeric
field validator for strings, floats, and integersisAlphaNumeric
field validator for stringsisInt
field validator for strings and integersisFloat
field validator for strings, floats, and integersisString
field validator for stringsisObject
field validator for objectsequals
field validator for any given data typecontains
field validator for stringsgreaterThan
field validator for numeric strings, floats, and integersgreaterEqual
field validator for numeric strings, floats, and integerslowerThan
field validator for numeric strings, floats, and integerslowerEqual
field validator for numeric strings, floats, and integersbetween
field validator for numeric strings, floats, and integersisDate
field validator for date strings and objects that implement the DateTimeInterface interfacedateHasFormat
field validator for date strings and objects that implement the DateTimeInterface interfacedateEquals
field validator for date strings and objects that implement the DateTimeInterface interfacedateLowerThan
field validator for date strings and objects that implement the DateTimeInterface interfacedateLowerEqual
field validator for date strings and objects that implement the DateTimeInterface interfacedateGreaterThan
field validator for date strings and objects that implement the DateTimeInterface interfacedateGreaterEqual
field validator for date strings and objects that implement the DateTimeInterface interfacedateBetween
field validator for date strings and objects that implement the DateTimeInterface interface
Installation
IMPORTANT: PHPValidation requires php version 8 or higher to work properly.
Usage
Obtaining a validator
A validator can be obtained through the ValidatorBuilder
class:
The validator builder has the following options:
Note: The builder already comes preconfigured with a strategy and validator class name, the example above just lists all possible configuration options.
Through a factory
A validator can also be obtained through the default factory:
Note: Adding more methods to this default factory class is possible by inheriting it.
Configuring array field validation
Within the validation builder, an array must be set that defines how a given array should be validated. This uses the following structure:
The structure itself is quite simple. It is just an array that defines the names of the fields through keys, and the field validation through classes that implement the FieldValidatorInterface
interface. Nested fields are also supported, you just have a key point to an array with the basic key => array\<FieldValidatorInterface> structure, this can be done infinitely.
Configuring custom error messages
Each built-in field validator comes with their own default error messages. These messages can be overriden by providing the builder with an array that has the following structure:
This structure needs a field name key that points to an array that has the field validator key and an error message as a key => value pair. Nested fields are also supported and can be used by wrapping the structure for a singular field, inside an array that gets pointed to by a key.
Using the validator
Once you have configured the validator builder, the validator can be built by using the build()
method:
The validator can then be used to validate an array:
Creating custom field validators
Creating a custom field validator is quite easy. The only thing you have to do is implement the FieldValidatorInterface
interface:
It is recommended to create a simple function that wraps your custom validator. PHPValidation comes with some basic built-in validators that are being wrapped in the following way:
Wrapping your custom validator with a function will prevent the following code from being written:
And makes the final code easier to read:
Available field validators
PHPValidation comes with some built-in field validators that should cover basic use cases.
required
When added, this field key must be present in an array.
notEmpty
When added, and the field exists, and the field is of the type string
or array
, it cannot be empty or contain only whitespace.
isArray
When added, and the field exists, it must be of the type array
.
hasKeys
When added, and the field exists, and the field is of the type array
, it must have all of the stated keys.
hasValues
When added, and the field exists, and the field is of the type array
, it must have all of the stated values.
confirmValues
When added, and the field exists, the field value must be equal to the value of another field. When specifying which array field value the current field must match, dot notation is used to denote the key tree: key1.nestedKey1
translates to the following php array:
in
When added, and the field exists, and the field is of the type string
, int
, float
, bool
, or array
, it can only contain one of the stated values.
Note: When this validator has an array value passed to it, it will only validate the first level of the array.
notIn
When added, and the field exists, and the field is of the type string
, int
, float
, or bool
, it cannot contain one of the stated values.
minLength
When added, and the field exists, and the field is of the type string
, it must have a minimum amount of characters.
maxLength
When added, and the field exists, and the field is of the type string
, it cannot have more than a certain amount of characters.
minCount
When added, and the field exists, and the field is of the type array
, it must have a minimum amount of values.
maxCount
When added, and the field exists, and the field is of the type array
, it cannot have more than a certain amount of values.
When added, and the field exists, and the field is of the type string
, it will check for an RFC 5322 compliant email address.
phoneNumber
When added, and the field exists, and the field is of the type string
, it will check for a valid international phone number.
Note: This validator does not accept phone numbers delimitted by any characters and/or whitespace.
Note: It probably will be better to create your own phone number validator since a phone number can differ greatly depending on the country/location.
isAlphabetic
When added, and the field exists, and the field is of the type string
, it will check if its value only contains normal, non-special, characters and whitespace.
isNumeric
When added, and the field exists, and the field is of the following types: string
, float
, or int
, it will check if the given value is numeric.
isAlphaNumeric
When added, and the field exists, and the field is of the type string
, it will check if the given value only contains normal, non-special, characters, numbers, and whitespace. The validator also supports whitelisting extra characters.
isInt
When added, and the field exists, and the field is of the following types: string
or int
, it will check if the given value can be converted to an integer, and thus is an integer.
isFloat
When added, and the field exists, and the field is of the following types: string
, float
, or int
, it will check if the given value can be converted to a float without losing any data, and thus is an float.
isString
When added, and the field exists, it will check if the given value is of the type string
.
isObject
When added, and the field exists, it will check if the given value is of the type object
.
objectOfType
When added, and the field exists, and the field is of the type object
, it will check if the given value has a desired object type.
equals
When added, and the field exists, it will check if the given value matches the field value. This validator supports strict and non-strict value validation.
contains
When added, and the field exists, and the field is of the type string
, it will check if the given value contains a specific substring.
greaterThan
When added, and the field exists, and the field is of the following types: string
, float
, or int
, it will check if the given value is greater than the field value.
greaterEqual
When added, and the field exists, and the field is of the following types: string
, float
, or int
, it will check if the given value is greater than, or equal to, the field value.
lowerThan
When added, and the field exists, and the field is of the following types: string
, float
, or int
, it will check if the given value is lower than the field value.
lowerEqual
When added, and the field exists, and the field is of the following types: string
, float
, or int
, it will check if the given value is lower than, or equal to, the field value.
between
When added, and the field exists, and the field is of the following types: string
, float
, or int
, it will check if the given value is between two given values.
isDate
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the field is a date string or an object that implements the DateTimeInterface
interface.
Note: This field uses php's strtotime()
function to check if a string is indeed a date string.
dateHasFormat
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the field uses a given php datetime format.
Note: It is recommended to only use this validator with date strings. Objects that implement the DateTimeInterface
interface will always be valid since they are internally cast back to a string with the given format.
dateEquals
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the field value is equal to a pre-determined date object.
dateLowerThan
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the given value is lower than the field value.
dateLowerEqual
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the given value is lower than, or equal to, the field value.
dateGreaterThan
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the given value is greater than the field value.
dateGreaterEqual
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the given value is greater than, or equal to, the field value.
dateBetween
When added, and the field exists, and the field is of the following type string
or implements the DateTimeInterface
interface, it will check if the given value is between two given dates.