Download the PHP package martynbiz/php-validator without Composer
On this page you can find all versions of the php package martynbiz/php-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download martynbiz/php-validator
More information about martynbiz/php-validator
Files in martynbiz/php-validator
Package php-validator
Short Description Basic validation for PHP forms
License MIT
Homepage http://github.com/martynbiz/php-validator
Informations about the package php-validator
Installation
Install with composer:
Basic Usage
The check() method instructs the object that this is a new value to validate. Additional methods are chained from this. Below shows an example of the object checking a value is not empty, then checking it is a valid email address.
To fetch the errors from a validation check, use getErrors():
After validation, check if data is valid:
Check if a data has been given. Useful for checking checkboxes have been ticked:
Note: will not work for empty strings, which isNonEmpty should also be used.
This will return an array of containing the error that occured (Email address is blank). Please note that although the isEmail method was called too, because it has already gathered an error it does not record another. Remember this when ordering your methods in the chain.
It is also possible to do this in one go, and return errors:
Optional fields are when validation only occurs on that param if the param is given.
Other methods for validating
The above example shows how to validate an email address but the following methods can be used to numeric, date and time stings too. Below is the full list of validation methods available.
Other error logging
You can use the logError() method too to log a custom error:
Extend Validator
Sometimes it's useful to add your own validator methods. This can be done by extending the class, and ensuring that your new method returns the instance logs the error:
Then you can chain the method as with the built in ones: