Download the PHP package cloudonaut/validator without Composer
On this page you can find all versions of the php package cloudonaut/validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package validator
Cloudonaut Validator
This is a simple reusable validator class for PHP.
Installation
Install with composer
Include it into your scripts by using composer autoload and create a new validator object.
Manueal installation
Just copy the Validator.php file from the src folder to your project and include it into your scripts. Than create a new validator object.
Basic usage
The validator stores violations automatically when there is a violation against a rule. To check is a validator has violations you can use the hasViolations() function. To get an array of the violations you can call getViolations(). To add violations you can use one of the many build in check functions or simply add a violation to the list using your own code to check the value.
Example using a build in check isNotEmpty
Check if a value is not empty. If it is empty a violation is added to the validator object.
Output
Example using addViolation
In this example we need the value to match 7 as a simple example. But you can build on this example to do other stuff with value. For example: Call another api and based on the result validate the value, check the value against a database table etc...
output
Example combining rules
You can check the same or other values as many as you want. For example. A e-mail adres must be filled in and a name. But a name is text only.
output
Basic functions
addViolation($msg='')
Adds a violation with the given message
hasViolations()
Checks if the validator has violations. It will return true when there are violations or false when there are no violations.
getViolations()
Returns an array of the messages of all the violations. If there are no violations it just returns an empty array.
Build in checks
All functions will also return true when no violation is added and false when the value violates the rule.
isNotEmpty($value, $msg='')
Adds a violation when the value is empty.
isText($value,$msg='')
Adds a violation when the value is not alphanumeric
isTextOnly($value,$msg='')
Adds a violation when the value is not containing only letters.
isNumber($value,$msg='')
Adds a violation when the value is not a number.
isDigit($value,$msg='')
Adds a violation when the value contains anything else than number characters.
isBetween($min, $max, $value, $msg='')
Adds a violation when the value is not between the min and max value.
isComplex($value,$msg='')
Adds a violation when the value is not containing:
- At least one lower case letter
- At least one upper case letter
- At least one number or symbol
- and should be between 8 and 20 characters long
isDate($value,$msg='')
Adds a violation when the value is not a date in the folowing format: yyyy-mm-dd
isTime($value,$msg='')
Adds a violation when the value is not a time indication in the folowing format: hh:mm:ss
isEmail($value,$msg='')
Adds a violation when the value is not an e-mail address
isURL($value,$msg='')
Adds a violation when the value is not an URL.
isIP($value,$msg='')
Adds a violation when the value is not an IP address. (v4)
isFilename($value,$msg='')
Adds a violation when the value is not a filename.
isInList($value,$list, $msg='')
Adds a violation when the value is not a given list.