
<?php /** * Class data validation (can be used to form validation) * * Class data validation (can be used to form validation). * Here is an example usage and available parameters. * Example of use: $formValidation->validate($pass, "required|matches[$pass_c]|min_length[6]"); * Possible parameters to use:: required, numeric, matches[comparison], min_length[number], max_length[number], exact_length[number], valid_email * * @version 1.0 * @author Jakub Kubera <jakubkubera1992@gmail.com> * @link www.jakubkubera.pl * @license New BSD License * @access public */ class ValidationData { private $name; /** * Assigning parameters to the class variables and run validation * * @param string $name Field name * @param string $rules Field validation rules * * @return bool Returns whether validation was successful */ public function validate($name, $rules) { $this->name = $name; $this->explodeRules($rules); return $this->run(); } /** * Returns whether validation was successful * * @return bool Returns whether validation was successful */ private function run() { if ($this->checkRequired() === false) $errors[] = 'required'; if ($this->checkNumeric() === false) $errors[] = 'numeric'; if ($key = $this->pregArrayKey('/matches\[(.*)]/')) { if ($this->checkMatches($confirm[1]) === false) $errors[] = $this->rules[$key]; } if ($key = $this->pregArrayKey('/min_length\[(.*)]/')) { if ($this->checkMinLength($value[1]) === false) $errors[] = $this->rules[$key]; } if ($key = $this->pregArrayKey('/max_length\[(.*)]/')) { if ($this->checkMaxLength($value[1]) === false) $errors[] = $this->rules[$key]; } if ($key = $this->pregArrayKey('/exact_length\[(.*)]/')) { if ($this->checkLength($value[1]) === false) $errors[] = $this->rules[$key]; } if ($this->checkEmail() === false) $errors[] = 'valid_email'; } /** * Separation of validation rules * * @param $rules Field validation rules */ private function explodeRules($rules) { } /** * Finds that there is a pattern in the rules, * like in_array with using regular expressions * * @param $pattern Search pattern in the table */ private function pregArrayKey($pattern) { } /** * Checks and returns whether the value is empty * * @return bool Returns whether the value is empty */ private function checkRequired() { } /** * Checks and returns whether the value is numeric * * @return bool Returns whether the value is numeric */ private function checkNumeric() { } /** * Checks and returns whether the specified phrases are the same * * @return bool Returns whether the specified phrases are the same */ private function checkMatches($confirm) { return ($this->name == $confirm) ? true : false; } /** * Checks and returns whether the value of a specified minimum length * * @return bool Returns whether the value of a specified minimum length */ private function checkMinLength($value) { } /** * Checks and returns whether the value of a specified maximum length * * @return bool Returns whether the value of a specified maximum length */ private function checkMaxLength($value) { } /** * Checks and returns whether the value of a specified length * * @return bool Returns whether the value of a specified length */ private function checkLength($value) { } /** * Checks and returns whether the given value is a valid email address * * @return bool Returns whether the given value is a valid email address */ private function checkEmail() { return (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $this->name)) ? true : false; } }
Przykład użycia:
<?php $formValidation = new ValidationData; $age = '123'; $name = 'Kuba'; $pass = 'test'; $pass_c = 'test'; $email = 'test1test.pl'; if ($formValidation->validate($email, "required|valid_email") === true) else { echo 'Blad, adres e-mail '.$email.' nie zostal poprawnie zwalidowany, ponizej bledy w tablicy $errors:<br />'; }