Validators
Crater PHP Framework comes with a standard set of validators, which are ready for you to use.
Types of validations
You have the following validators:
- date
- amount
- number
- alfanum
- not_empty
- words
- phone
- zipcode
- plate
- price
- 2digitopt
- 2digitforce
- anything
How to use
The constructor of the validation class has 3 parameters:
$validator = new \Core\Helpers\Validator($validation, $required, $sanatation);
All parameters are array.
Examples
$validations = array( 'name' => 'anything', 'email' => 'email', 'alias' => 'anything', 'pwd'=>'anything', 'gsm' => 'phone', 'birthdate' => 'date' ); $required = array('name', 'email', 'alias', 'pwd'); $sanatation = array('alias'); $validator = new \Core\Helpers\Validator($validations, $required, $sanatation); if ($validator->validate($this->getPost())) { $correctsData = $validator->getData(); } else { $errors = $validator->getErrors(); }
Static methods
Also, validator class has two static methods for validate and sanitize for single validate/sanatize
$var = 'crater@domain.com'; $type = 'email'; $isValid = \Core\Helpers\Validator::validateItem($var, $type); $sanitize = \Core\Helpers\Validator::sanatizeItem($var, $type)↑